mysql - How do I do IN queries in Go? -
i'd run query in go using "database/sql" package:
select id, name users id in (1,2,3,4);
how use variable substitution in query command? e.g., want (which of course doesn't work):
db.query("select id, name users id in (?)", []int{1,2,3,4})
@volker suggests in comments database drivers may able expand slice you. driver can handle slice elements individually, however:
db.query("select id, name users id in (?, ?, ?, ?)", 1, 2, 3, 4)
of course, you'd need know how many elements going in slice when write that, isn't case. generating correct number of question marks simple enough , gives general solution:
ints := []interface{}{1,2,3,4} marks := strings.repeat("?,", len(ints) - 1) + "?" db.query("select id, name users id in (" + marks + ")", ints...)
if slice might empty, you'll have handle somehow.
Comments
Post a Comment