mysql where子句不起作用

时间:2014-07-12 06:07:33

标签: mysql where

我有这个MySQL查询只为那些有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 '%سرپل%' 

问题:查询检索了那些有另一种语言的数据,但我提到了语言=' 3'

截图:

enter image description here

5 个答案:

答案 0 :(得分:2)

您必须为ANDOR条件添加括号以满足您的条件,如下所示

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

答案 1 :(得分:1)

这是因为AND的优先级高于OR,因此您的WHERE子句被解释为:

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

语言测试仅与第一个LIKE合并,而不是与其他WHERE language = 3 AND (noor_content.keywords LIKE '%سمنگان%' OR noor_content.keywords LIKE '%جوزجان%' OR noor_content.keywords LIKE '%سرپل%') 合并。您需要添加括号:

{{1}}

答案 2 :(得分:0)

将条件放在括号中。

 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 '%سرپل%' ));

答案 3 :(得分:0)

使用括号搜索关键字

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 '%سرپل%')

答案 4 :(得分: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 '%سرپل%' )