Masterminds / squirrel

Fluent SQL generation for golang
Other
6.88k stars 465 forks source link

Multiple value where clause using squirrel #371

Open AshishMittal02 opened 10 months ago

AshishMittal02 commented 10 months ago

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 commented 10 months ago

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

ysomad commented 9 months ago

you can use squirrel.And

harissudrajat commented 3 months ago

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"}})