Mysql查询忽略NULL和空字段

时间:2012-02-07 22:23:49

标签: php mysql

我知道一个非常疯狂的查询,但是当我运行它时,它会返回所有匹配,无论字段mvideo是否为空。

查询是:

SELECT * 
FROM `weaponsprojects` 
WHERE mvideo != "" 
  AND credit7 = "' .$person. '" 
   OR credit2 = "' .$person. '" 
   OR credit3 = "' .$person. '" 
   OR credit4 = "' .$person. '" 
   OR credit5 = "' .$person. '"
   OR credit6 = "' .$person. '"
   OR credit1 = "' .$person. '"
   OR credit8 = "' .$person. '"
   OR credit9 = "' .$person. '"
   OR credit10 = "' .$person. '"
   OR credit11 = "' .$person. '"
   OR credit12 = "' .$person. '"
   ORDER BY usort'

并且当任何信用等于该人但它完全忽略mvideo部分时它返回。这是为什么?

编辑:该字段不是NULL,只是简单地清空空格

1 个答案:

答案 0 :(得分:4)

我认为WHERE条件在逻辑上有缺陷......你需要一些优先权,如下:

SELECT * 
FROM `weaponsprojects` 
WHERE mvideo != "" 
  AND (credit7 = "' .$person. '" 
   OR credit2 = "' .$person. '" 
   OR credit3 = "' .$person. '" 
   OR credit4 = "' .$person. '" 
   OR credit5 = "' .$person. '"
   OR credit6 = "' .$person. '"
   OR credit1 = "' .$person. '"
   OR credit8 = "' .$person. '"
   OR credit9 = "' .$person. '"
   OR credit10 = "' .$person. '"
   OR credit11 = "' .$person. '"
   OR credit12 = "' .$person. '")
   ORDER BY usort'

添加()我们告诉WHERE语句:“mvideo不是空的,并且与OR个语句之一匹配“