2016-06-07 60 views
1

这可能是非常基本的问题,但我找不到解决方案。我需要选择其中至少有一个值不为0或空的记录。我检查3列。MySQL - 仅当至少一个值不为空或空值时才选择行

我的代码:

$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

你是非常接近,只需要使用一些地方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')) 
+2

或短...'AND(h1col NOT IN( '', '0')或h2col不是('','0')或者......' – spencer7593

+0

这两个建议都起作用了。我选择了@ spencer7593中的一个更短。很好,非常感谢你。我已经知道我将使用“NOT IN”其他地方。 – Nita

相关问题