2013-07-28 93 views
0

我想写一个sql查询,如果特定列的总和为零,那么结果将被检索。我已经尝试如下:(我只写了检查部分,因为错误只是为了这部分)。与mysql中的整数值比较

query=.... WHERE SUM(column1)='0' 

coumn1有整型数据类型。我收到以下错误:

java.sql.SQLException: Invalid use of group function 
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:946) 

任何帮助,高度赞赏。谢谢。

回答

3

你的查询是错误的。如果要使用sum()等集合函数,则应该使用having子句。

The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregate functions.

看那MySql documentation of SELECT

The WHERE clause, if given, indicates the condition or conditions that rows must satisfy to be selected. where_condition is an expression that evaluates to true for each row to be selected. The statement selects all rows if there is no WHERE clause.

In the WHERE expression, you can use any of the functions and operators that MySQL supports, except for aggregate (summary) functions.

+0

感谢,为我工作,对功能,如SUM(),而不是WHERE子句HAVING应该被使用。以下是工作查询 query = ... HAVING SUM(column1)= 0 – user2625279

1

HAVING子句在SELECT语句用来指定行或聚集体的组过滤器条件。

MySQL HAVING子句通常与GROUP BY子句一起使用。使用GROUP BY子句时,可以将过滤条件应用于出现在GROUP BY子句中的列。如果省略GROUP BY子句,则MySQL HAVING子句的行为与WHERE子句相同。请注意,MySQL HAVING子句将条件应用于每个行组,而WHERE子句将条件应用于每个单独的行。

使用HAVING在您的查询与聚合函数:

query=.... HAVING SUM(column1)='0';