MySQL查询“哪里SUM(价格)大于等于”?

时间:2011-10-12 09:10:03

标签: php mysql

这是我的查询:

SELECT user,name,SUM(price) FROM $tableName WHERE faktor='1' GROUP BY user ORDER BY SUM(price) ASC

此查询工作,但我想显示谁有总和(价格)大于100 $。 我用WHERE SUM(Price) < '100'测试它但不起作用,结果是错误的。

谢谢;)

4 个答案:

答案 0 :(得分:7)

您需要使用HAVING,而不是WHERE

SELECT user,name,SUM(price)
  FROM $tableName
  WHERE faktor='1'
  GROUP BY user
  HAVING SUM(price) < 100
  ORDER BY SUM(price) ASC
在进行分组之前,

WHERE适用。 HAVING适用于后缀。

答案 1 :(得分:2)

要评估查询中累积函数的值,您需要使用查询的HAVING clause

答案 2 :(得分:1)

尝试使用HAVING

SELECT user,name,SUM(price) FROM $tableName WHERE faktor='1'
GROUP BY user
HAVING SUM(Price) < 100
ORDER BY SUM(price) ASC

答案 3 :(得分:1)

查找HAVING - sql子句。它充当WHERE但适用于GROUP

SELECT user,name,SUM(price) 
FROM $tableName 
WHERE faktor='1' 
GROUP BY user 
HAVING SUM(price) > 100
ORDER BY SUM(price) ASC