crelrm1
crelrm1

Reputation: 5

SQL request - search query not working

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

Answers (3)

michalczukm
michalczukm

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

XN16
XN16

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

T I
T I

Reputation: 9933

Use paranthesis

SELECT * FROM items 
WHERE (`name` LIKE '%yellow%' OR `description` LIKE '%yellow%') AND `language` = 'en-GB';

Upvotes: 1

Related Questions