SQL Expressions: IN
.In()
is a method that exists on all Fields and also sq.RowValue
(see Tuple IN).
It returns a Predicate, which means it can be used anywhere where a Predicate is expected.
Note that when literal values are passed into it (for example in a slice) the values are parameterized, not literally interpolated (IN ($1, $2, $3)
and not IN (1, 2, 3)
).
u := tables.USERS().As("u")
u.USER_ID.In(sq.RowValue{u.NAME, u.EMAIL, u.PASSWORD})
u.USER_ID.In(sq.RowValue{1, 2, 3})
In follows the slice expansion rules, so slices are viable arguments.
u := tables.USERS().As("u")
userIDs := []int{1, 2, 3}
u.USER_ID.In(userIDs)
names := []int{"tom", "dick", "harry"}
u.NAME.In(names)
u := tables.USERS().As("u")
u.USER_ID.In(sq.
Select(u.USER_ID).
From(u).
Where(u.Name.IsNotNull()),
)
u := tables.USERS().As("u")
sq.RowValue{u.USER_ID, u.NAME}.In{sq.RowValues{
sq.RowValue{1, "tom"},
sq.RowValue{2, "dick"},
sq.RowValue{3, "harry"},
}}
sq.RowValue{u.USER_ID, u.NAME, u.EMAIL}.In(sq.
Select(u.USER_ID, u.NAME, u.EMAIL).
From(u).
Where(u.NAME.IsNotNull()),
)