Go在这个SQL查询中为第二个参数做了什么。
我试图在postgres中使用IN查找。
我试图在postgres中使用IN查找。
stmt,err := db.Prepare("SELECT * FROM awesome_table WHERE id= $1 AND other_field IN $2") rows,err := stmt.Query(10,???)
我真正想要的
SELECT * FROM awesome_table WHERE id=10 AND other_field IN (this,that);
查询只需要varargs来替换sql中的params
所以,在你的例子中,你只会做
所以,在你的例子中,你只会做
rows,err := stmt.Query(10)
说,这和你的第二个例子是动态的,那么你会这样做
stmt,err := db.Prepare("SELECT * FROM awesome_table WHERE id=$1 AND other_field IN ($2,$3)") rows,"this","that")
如果您有“IN”部分的变量args,可以执行(play)
package main import "fmt" import "strings" func main() { stuff := []interface{}{"this","that","otherthing"} sql := "select * from foo where id=? and name in (?" + strings.Repeat(",?",len(stuff)-1) + ")" fmt.Println("sql:",sql) args := []interface{}{10} args = append(args,stuff...) fakeExec(args...) // This also works,but I think it's harder for folks to read //fakeExec(append([]interface{}{10},stuff...)...) } func fakeExec(args ...interface{}) { fmt.Println("Got:",args) }