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

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -