开发者

MySQL Query for " Where SUM(Price) Bigger Than Equal " ?

This is My Query :

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

This Query Work , but i want to Show Who开发者_Python百科 Have Sum(price) Bigger Than 100 $ . i test it with WHERE SUM(Price) < '100' but not work , and Result is wrong.

Thanks ;)


You need to use HAVING, not WHERE.

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

WHERE applies before doing the group-by. HAVING applies afterwords.


To evaluate values from accumulative functions in a query you need to use the HAVING clause of the query.


Lookup the HAVING-sql clause. It acts as a WHERE but is applied to a GROUP:

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


Try using HAVING

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜