Hello,
I've got two tables:
users
---------
id | name
---------
1 | John
2 | Bob
id ... unique ID number of the user
name ... Name of the user
and
sales
-----------------
id | user| amount
-----------------
1 | 1 | 10.00
2 | 2 | 13.00
3 | 1 | 9.40
4 | 1 | 20.15
id ... unique sale ID number
user ... user ID, same as "id" in "users" table
amount ... total sale amount
Now, I can get the total sales amount for each user like this:
SELECT u.id, sum( s.amount ) AS total
FROM users AS u, sales AS s
WHERE u.id = s.user
GROUP BY u.id
LIMIT 0 , 30
This returns
id | total
----------
1 | 43.15
2 | 9.40
which is correct.
But what I need to do is return only those users, who have total in sales at least 10.00. I tried:
SELECT u.id, sum( s.amount ) AS total
FROM users AS u, sales AS s
WHERE u.id = s.user
AND total >= 10.00
GROUP BY u.id
LIMIT 0 , 30
but I get error "unknow colum total". I can't figure out a way to do it. Any suggestions how to handle this?
Thanks in advance!
I've got two tables:
users
---------
id | name
---------
1 | John
2 | Bob
id ... unique ID number of the user
name ... Name of the user
and
sales
-----------------
id | user| amount
-----------------
1 | 1 | 10.00
2 | 2 | 13.00
3 | 1 | 9.40
4 | 1 | 20.15
id ... unique sale ID number
user ... user ID, same as "id" in "users" table
amount ... total sale amount
Now, I can get the total sales amount for each user like this:
SELECT u.id, sum( s.amount ) AS total
FROM users AS u, sales AS s
WHERE u.id = s.user
GROUP BY u.id
LIMIT 0 , 30
This returns
id | total
----------
1 | 43.15
2 | 9.40
which is correct.
But what I need to do is return only those users, who have total in sales at least 10.00. I tried:
SELECT u.id, sum( s.amount ) AS total
FROM users AS u, sales AS s
WHERE u.id = s.user
AND total >= 10.00
GROUP BY u.id
LIMIT 0 , 30
but I get error "unknow colum total". I can't figure out a way to do it. Any suggestions how to handle this?
Thanks in advance!