Masterminds / squirrel

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

Multiple value where clause using squirrel #371

Open AshishMittal02 opened 11 months ago

AshishMittal02 commented 11 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 11 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 11 months ago

you can use squirrel.And

harissudrajat commented 5 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"}})