Reputation: 5
In sql table i have fields: name, description and language
my language field has values like: "en-GB" or "fr-FR".
SELECT * FROM items WHERE `name` LIKE '%yellow%' OR `description` LIKE '%yellow%' AND `language` = 'en-GB';
Results with search term yellow are ok. but i get also results who has langauge fr-FR, but in my query i have set as language en-GB.
I have searched for query on w3c, and i think part with "like" is true.
What i do here wrong?
Upvotes: 0
Views: 73
Reputation: 10459
get language
to front of where
SELECT * FROM items WHERE `language`='en-GB' AND (`name` LIKE '%yellow%' OR `description` LIKE '%yellow%');
Upvotes: 0
Reputation: 5879
You need to use brackets as the 'OR' operated is evaluated first, where you are expecting the 'AND' operator to be.
SELECT *
FROM items
WHERE (`name` LIKE '%yellow%' OR `description` LIKE '%yellow%')
AND `language` = 'en-GB'
Upvotes: 1
Reputation: 9933
Use paranthesis
SELECT * FROM items
WHERE (`name` LIKE '%yellow%' OR `description` LIKE '%yellow%') AND `language` = 'en-GB';
Upvotes: 1