PHP / MySQL - 其中列IS不为0 / NULL

时间:2013-08-21 07:26:13

标签: php mysql

我正在尝试运行此SQL:

$sql="SELECT LEAST(".implode(',',$column_list).") as num FROM callplandata WHERE num IS NOT 0 ";

错误:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '0' at line 1

我也试过

$sql="SELECT LEAST(".implode(',',$column_list).") as num FROM callplandata WHERE num IS NOT NULL ";

错误:Unknown column 'num' in 'where clause'

但是每个人都会遇到上述错误。

我怎么能绕过这个 - 我试图显示非0的值

1 个答案:

答案 0 :(得分:3)

在这种情况下,请使用HAVING代替WHERE

$sql="SELECT LEAST(".implode(',',$column_list).") as num FROM callplandata HAVING num != 0 ";

差异是什么?在WHERE操作之前应用GROUP BYLEAST也必须在返回之前对结果进行分组(因此在计算结果之前调用WHERE子句),HAVING已应用之后(并且可以过滤MIN, MAX, LEAST等聚合。)