包括等于或大于正确的sql

时间:2014-05-16 14:45:36

标签: mysql sql

我正在尝试在从JSP模板调用的这个sql语句中添加一个等于或大于20.0的参数,但它在HAVING总和(金额)附近一直给我sql语法错误:

 "SELECT performId,sum(amount) AS totalAmount FROM performTransactions"+ 
 " WHERE  time >= STR_TO_DATE('" + (ftd.format(timeFrom)) + "', '%Y.%m.%d %H:%i:%s')" + 
 " AND time < STR_TO_DATE('" + (ftd.format(timeTo)) + "', '%Y.%m.%d %H:%i:%s')" + 
 " AND type IN (1, 2)  GROUP BY performId ORDER BY totalAmount DESC" + 
 " HAVING sum(amount) >= 20.0" ;

我试图将参数包含在不同的位置,但是发送“无效使用组功能”,如何正确设置sum(amount)或totalAmount大于20.0? 谢谢

1 个答案:

答案 0 :(得分:2)

HAVING在ORDER BY之前出现:

"SELECT performId,sum(amount) AS totalAmount FROM performTransactions"+ 
 " WHERE  time >= STR_TO_DATE('" + (ftd.format(timeFrom)) + "', '%Y.%m.%d %H:%i:%s')" + 
 " AND time < STR_TO_DATE('" + (ftd.format(timeTo)) + "', '%Y.%m.%d %H:%i:%s')" + 
 " AND type IN (1, 2)  GROUP BY performId " + 
 " HAVING sum(amount) >= 20.0" 
 " ORDER BY totalAmount DESC";