MySQL - 仅当至少有一个值不为null或为空时才选择行

时间:2016-06-07 23:39:09

标签: mysql

这可能是非常基本的问题,但我无法找到它的解决方案。我需要选择其中至少有一个值不为0或为空的记录。我查了3列。

我的代码:

$results = $mysqli->query("SELECT * FROM table
  WHERE datestamp>='$startdate' AND datestamp<='$finishdate'
      AND ((h1col !='' OR h1col !='0') OR  (h2col !='' OR h2col !='0')
                     OR (h3col !='' OR h3col !='0'))");

请你把我放在正确的轨道上。谢谢。总是愿意学习。 (:

1 个答案:

答案 0 :(得分:1)

你非常接近,只需要在某些地方使用and而不是or

SELECT * 
FROM table 
WHERE datestamp>='$startdate' 
    AND datestamp<='$finishdate' 
    AND ((h1col !='' AND h1col !='0') OR 
         (h2col !='' AND h2col !='0') OR 
         (h3col !='' AND h3col !='0'))
相关问题