php - Mysql查询忽略NULL和空字段

标签 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,只是简单地留空,没有空格,只是空白

最佳答案

我认为 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 语句之一”

关于php - Mysql查询忽略NULL和空字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9184922/

相关文章:

php - Ajax 被 Wordpress 警告 : solve trough ob_start()? 破坏

php - 通过 mySQL 或 php 获得时间戳更好吗?

mysql - org.hibernate.exception.GenericJDBCException : could not insert

php - WordPress如何覆盖wc_admin_report中的函数get_order_report_data

php - 写入文本文件 (PHP)

php - 最好的选择是使用缩略图创建器吗?还是更好地使用样式?图像的最大尺寸

php - pg_query_params 因参数过多而失败

简单日期查询中的 MySQL phpMyAdmin 错误

mysql - 如果过程中给出 null,则不更新

python - 如何提高 django mysql 复制性能?