Prashant Arvind
Prashant Arvind

Reputation: 2347

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.

Upvotes: 1

Views: 1817

Answers (2)

Md. Asaduzzaman
Md. Asaduzzaman

Reputation: 15433

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%'

Upvotes: 3

Ajay K S
Ajay K S

Reputation: 360

Try this

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

Upvotes: 1

Related Questions