squirrel icon indicating copy to clipboard operation
squirrel copied to clipboard

Multiple value where clause using squirrel

Open AshishMittal02 opened this issue 1 year ago • 3 comments

I'm trying to recreate this query with squirrel:

SELECT c.userID, c.name, o.orderID FROM users as c join order as o
    ON c.userID = o.userID where orderID IN ('a', 'b');

The closest I can come up with is this and it's wrong:

query := sq.Select("c.id, c.name, o.orderID").
	From("users").join("order as o using (userID)")
	Where(sq.Eq{
		"id": []string{'a', 'b'},
	})

AshishMittal02 avatar Dec 08 '23 07:12 AshishMittal02

One option is there to have multiple Where(sq.Eq{} but if i have 100 orderIDs it will grow alot and i think it will be ugly

AshishMittal02 avatar Dec 08 '23 07:12 AshishMittal02

you can use squirrel.And

ysomad avatar Dec 16 '23 11:12 ysomad

You can use this code

sq.StatementBuilder.PlaceholderFormat(sq.Colon).Select("c.userID, c.name, o.orderID").From("users AS c").
		Join("orders AS o ON c.orderID = o.orderID").
		Where(sq.Eq{"orderID": []string{"a", "b"}})

harissudrajat avatar Jun 10 '24 08:06 harissudrajat