我正在尝试编写一个采用列表参数(即,作为值列表的单个参数)的查询。看来这至少有时在 Postgres 中是可能的(https://stackoverflow.com/a/10829760/836390)。我想要的是这样的:
rows, err := db.Query("SELECT * FROM table WHERE id in $1", []int{1, 2, 3})
但是,当我使用 pq 执行此操作时驱动程序,我得到一个错误:
sql: converting Exec argument #0's type: unsupported type []int, a slice
这是 pq
还不支持,还是 database/sql
不支持,或者 Postgres 根本不支持,还是什么?谢谢!
最佳答案
您可以使用 pq.Array现在有 slice 参数。所以查询看起来像:
rows, err := db.Query("SELECT * FROM table WHERE id in $1", pq.Array([]int{1, 2, 3}))
关于sql - Go 中的 Postgres 列表参数(使用 database/sql 和 pq),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20928212/