Root
Root

Reputation: 2349

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 Have Sum(price) Bigger Than 100 $ . i test it with WHERE SUM(Price) < '100' but not work , and Result is wrong.

Thanks ;)

Upvotes: 1

Views: 5996

Answers (4)

Jim
Jim

Reputation: 22646

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

Upvotes: 1

0xCAFEBABE
0xCAFEBABE

Reputation: 5666

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

Upvotes: 2

PatrikAkerstrand
PatrikAkerstrand

Reputation: 45721

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

Upvotes: 1

derobert
derobert

Reputation: 51137

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.

Upvotes: 7

Related Questions