Search code examples
androidandroid-sqliteandroid-room

How to write select query for multiple column in a room database?


I want to get filtered record base on multiple columns("race","flavors").

You can see below image of database where red box is item_type and blue box where you can see item_flavors. enter image description here

Now I wanted to get all "sativa" (item_type) with all of this "Citrus","Berry","Nutty"(item_flavors). And for this I wrote this query.

SELECT * FROM StrainModel WHERE race LIKE 'sativa' AND flavors LIKE '%Berry%' OR flavors LIKE '%Citrus%' OR flavors LIKE '%Nutty%'

But this query was wrong and give me all three type of data("sativa","indica","hybrid"), but I just wanted "sativa" data.


Solution

  • Try using

    SELECT * FROM StrainModel WHERE race = 'sativa' AND (flavors LIKE '%Berry%' OR flavors LIKE '%Citrus%' OR flavors LIKE '%Nutty%')
    

    Instead of

    SELECT * FROM StrainModel WHERE race LIKE 'sativa' AND flavors LIKE '%Berry%' OR flavors LIKE '%Citrus%' OR flavors LIKE '%Nutty%'