Room - Select query with IN condition?
Hi you can use this query:
@Query("SELECT * FROM user WHERE uid IN(:userIds)")
public abstract List findByIds(int[] userIds);
or
@Query("SELECT * FROM user WHERE uid IN(:userIds)")
public abstract List findByIds(List<Integer> userIds);
So as I was preparing to submit this, I double-checked a bunch of the stuff I had looked up previously and found the thing I had somehow missed and would have saved this question from being necessary.
As it turns out, both of these options:
- Passing the argument as a
List<String>
- Passing the argument as a
String[]
are viable (and you can replace String
with any type the database can represent, such as char
or int
), you simply need to change the syntax in the @Query
annotation from this:
@Query("SELECT * FROM Table WHERE column IN :filterValues")
to this:
@Query("SELECT * FROM Table WHERE column IN (:filterValues)")
Easy as pie, right?
Note that the third method above (passing the argument as simply a String
, but formatted as (value_1, value_2, ..., value_n)
) does not appear to be supported by Room, but that's probably not a bad thing, since that's the hard way.
Since I already had the whole thing typed out, I figured I would leave the question up in case other people are have as much difficulty finding this solution as I did and stumble upon this question.