【问题标题】:Golang use array values in db query to filter recordsGolang在db查询中使用数组值过滤记录
【发布时间】:2021-12-06 08:29:02
【问题描述】:

我有数组 int64 值的列表

ids = [{1} {2} {3}]

我想在db查询中使用上面的数组来过滤掉id不在上面ids中的记录。

SELECT * from table where id not in (1,2,3);

我尝试了很多方法,但未能生成查询字符串。

【问题讨论】:

  • 你能举一个例子来说明你是如何解决这个问题的吗?并提供像ids := []int{1,2,3} 这样的有效语法

标签: go goquery go-structtag


【解决方案1】:

我创建了一个示例场景如下:

func main() {
    ids := []int{1, 2, 3}

    var tmp []string

    for _, v := range ids {
        tmp = append(tmp, fmt.Sprint(v))
    }

    query := "SELECT * from table where id not in (" + strings.Join(tmp, ",") + ")"
    fmt.Println(query)

}

你可以在 go playground link运行它

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-15
    • 2018-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多