如何从地图接口返回所有行?

huangapple go评论67阅读模式
英文:

How to return all rows from a map interface

问题

基本上,我想返回查询中特定值的所有行,查询如下所示:

var listOf []map[string]interface{}

query2 = strings.Replace(query2, "listOfIds", fmt.Sprintf("%v", listOf[0]["itemIds"]), -1)

目前,上述代码返回了预期的第一行,因为使用了listOf[0],但是如何返回所有行呢?有没有办法做到这一点?

编辑:

为了提供更多背景信息,这里有两个查询。查询1生成了id,然后将其传递给查询2:

whereQuery := " WHERE ssItemStoreId = itemIds AND ssItemStoreId = clId" + whereVariables + " AND itemClosedReason != 'Duplicated' AND clId != '' AND ItemArchived = 0"

query1 = query1 + whereQuery

// 数据变量
var listOf []map[string]interface{}

结果被传递给s.makeSqlQuery(query1, &listOf),然后传递给查询2,但是有数百行数据,我只能检索到一行。我尝试使用range,但是得到了index out of range [0] with length 0的错误。

query2 = strings.Replace(query2, "listOfIds", fmt.Sprintf("%v", listOf[0]["itemIds"]), -1)

谢谢!

英文:

Basically, I want to return all rows of a specific value in the query which looks as the following

var listOf []map[string]interface{}

query2 = strings.Replace(query2, "listOfIds", fmt.Sprintf("%v", listOf[0]["itemIds"]), -1)

at the moment the above returns first row as expected since listOf[0] but how can I return all of them, is there a way to do this?

EDIT:

To give a bit more context, there are 2 queries. Query 1 generates the ids which then are being past on query 2

whereQuery := " WHERE ssItemStoreId = itemIds AND ssItemStoreId = clId" + whereVariables + " AND itemClosedReason != 'Duplicated' AND clId != '' AND ItemArchived = 0"

query1 = query1 + whereQuery

//Data Variables
var listOf []map[string]interface{}
		

The results are being passed on s.makeSqlQuery(query1, &listOf) which are being passed on query 2 but there are hundreds of rows and I am only retriving one. I tried using range but I get index out of range [0] with length 0

query2 = strings.Replace(query2, "listOfIds", fmt.Sprintf("%v", listOf[0]["itemIds"]), -1)

Thank you!

答案1

得分: 1

回答这个问题,提供的上下文不足够,但我会添加一些答案。

你可以迭代"listOf"映射,并为每个项目构建单独的查询,然后在数据库中执行。
或者迭代映射并将"itemIds"分别附加到查询中的逗号(,)中,然后在数据库中执行。

示例1:

//你的方式
var listOf []map[string]interface{}

listOf = append(listOf, map[string]interface{}{})

listOf[0]["1"] = "1"
listOf[0]["3"] = "3"

query2 := "select * from sometable where id= %s"
ids := ""
for _, v := range listOf {
    for _, v1 := range v {
        // 替换逻辑
        if ids == "" {
            ids = fmt.Sprintf(`%v`, v1)
            continue
        }
        ids += fmt.Sprintf(`,%v`, v1)
    }
}

query2 = fmt.Sprintf(query2, ids) // select * from sometable where id= 3,1
fmt.Println(query2)

示例2:

// 简单方式
listOf := make(map[string]interface{})

listOf["1"] = "1"
listOf["2"] = "2"

query2 := "select * from sometable where id= %s"
ids := ""
for _, v := range listOf {
    // 替换逻辑
    if ids == "" {
        ids = fmt.Sprintf(`%v`, v)
            continue
        }
        ids += fmt.Sprintf(`,%v`, v)

    }

    query2 = fmt.Sprintf(query2, ids) // select * from sometable where id= 1,2

    fmt.Println(query2)
英文:

To answer this question. provided context isn't enough.but i will add some answer.

You can iterate the "listOf" map and build separate query for each item and execute in db.
Or iterate map and append "itemIds" in to query comman(,) separately and execute in db.

ex 1:

	//Your Way
var listOf []map[string]interface{}

listOf = append(listOf, map[string]interface{}{})

listOf[0]["1"] = "1"
listOf[0]["3"] = "3"

query2 := "select * from sometable where id= %s"
ids := ""
for _, v := range listOf {
	for _, v1 := range v {
		// replace logic
		if ids == "" {
			ids = fmt.Sprintf(`%v`, v1)
			continue
		}
		ids += fmt.Sprintf(`,%v`, v1)
	}
}

query2 = fmt.Sprintf(query2, ids) // select * from sometable where id= 3,1
fmt.Println(query2)

ex 2:

 //  Easy Way
	listOf := make(map[string]interface{})

	listOf["1"] = "1"
	listOf["2"] = "2"

	query2 := "select * from sometable where id= %s"
	ids := ""
	for _, v := range listOf {
		// replace logic
		if ids == "" {
			ids = fmt.Sprintf(`%v`, v)
			continue
		}
		ids += fmt.Sprintf(`,%v`, v)

	}

	query2 = fmt.Sprintf(query2, ids) // select * from sometable where id= 1,2

	fmt.Println(query2)

huangapple
  • 本文由 发表于 2022年9月1日 16:47:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/73566570.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定