2

i have this MySQL query to retrieve some data just for those who have language = 3

  SELECT
  noor_content.id,noor_content.body,noor_content.category,noor_content.introtext,noor_content.title,noor_content.keywords,noor_content.language,path 
  FROM (`noor_content`) 
  join noor_categories 
  on (noor_content.category = noor_categories.id) 
  WHERE `language` = '3' 
  AND noor_content.keywords LIKE '%سمنگان%' 
  or noor_content.keywords LIKE '%جوزجان%' 
  or noor_content.keywords LIKE '%سرپل%' 

Problem: the query retrieved those data that have another language but i mentioned language='3'

Screenshot:

enter image description here

Bahrami-Reza
  • 608
  • 2
  • 7
  • 24
  • use `WHERE language = 3 AND (noor_content.keywords LIKE '%سمنگان%' or noor_content.keywords LIKE '%جوزجان%' or noor_content.keywords LIKE '%سرپل%')` – Tamil Selvan C Jul 12 '14 at 06:09

5 Answers5

2

You have to put bracket for AND and ORcondition to satisfy your condition like below

 WHERE `language` = '3' 
  AND (noor_content.keywords LIKE '%سمنگان%' 
  or noor_content.keywords LIKE '%جوزجان%' 
  or noor_content.keywords LIKE '%سرپل%' );
Sadikhasan
  • 18,365
  • 21
  • 80
  • 122
1

It's because AND has higher precedence than OR, so your WHERE clause is being interpreted as:

WHERE (language = 3 AND noor_content.keywords LIKE '%سمنگان%')
OR noor_content.keywords LIKE '%جوزجان%' 
OR noor_content.keywords LIKE '%سرپل%'

The language test is only combined with the first LIKE, not the others. You need to add parentheses:

WHERE language = 3 
AND (noor_content.keywords LIKE '%سمنگان%'
    OR noor_content.keywords LIKE '%جوزجان%' 
    OR noor_content.keywords LIKE '%سرپل%')
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

put conditions in brackets.

 SELECT noor_content.id,noor_content.body,noor_content.category,noor_content.introtext,noor_content.title,noor_content.keywords,noor_content.language,path 
  FROM (`noor_content`) 
  join noor_categories 
  on (noor_content.category = noor_categories.id) 
  WHERE (`language` = '3' )
  AND ((noor_content.keywords LIKE '%سمنگان%') 
  or (noor_content.keywords LIKE '%جوزجان%') 
  or (noor_content.keywords LIKE '%سرپل%' ));
JayKandari
  • 1,228
  • 4
  • 16
  • 33
  • 1
    Hey Jai tnx man, after i found my first prob, you did answer my second prob. actually i used for loop and i have if and elseif condition and you did answer my sec prob by double (( after AND. – Bahrami-Reza Jul 12 '14 at 06:32
  • it is best practice to use brackets for each condition. They can be segregated easily. \ – JayKandari Jul 12 '14 at 06:36
0

Use bracket for search keywords

SELECT noor_content.id,noor_content.body,noor_content.category,noor_content.introtext,noor_content.title,noor_content.keywords,noor_content.language,path 
  FROM `noor_content` 
  join noor_categories 
  on noor_content.category = noor_categories.id
WHERE language = 3 AND 
(noor_content.keywords LIKE '%سمنگان%' or 
noor_content.keywords LIKE '%جوزجان%' 
or noor_content.keywords LIKE '%سرپل%')
Tamil Selvan C
  • 19,913
  • 12
  • 49
  • 70
0
Because you are using OR operator between conditions so use Brackets() 
like this then try

WHERE `language` = '3' 
AND ( noor_content.keywords LIKE '%سمنگان%' 
OR noor_content.keywords LIKE '%جوزجان%'  
OR noor_content.keywords LIKE '%سرپل%' )