I have two tables: users, images. The users has a column paid which is a digit for how many images they paid to enter into the contest..
images has a column entered with a value of 0 for not entered, and 1 for entered. I need the sum of these for each user.
I am trying to query all users who paid for more images than they entered. but it’s not working correctly.. heres what prints:
bob@example.com 8 2
jcannon@example.com 3 3
jrcih@example.com 7 3
aerwqeerll@example.com 5 1
ray@example.com 2 3
You can see that jcannon@ and ray@ should not be in the list, because paid is not greater than entered
here is the code:
$SQL = mysql_query("SELECT *, SUM(images.entered)
FROM users, images
WHERE users.id=images.owner
AND users.paid>'SUM(images.entered)'
GROUP BY users.id ");
while($sendto=mysql_fetch_array($SQL)){
print $sendto['email']." ".$sendto['paid'].
" ".$sendto['SUM(images.entered)']. "<br>";
}
What am I doing wrong?
First get rid of the quotation marks, they are for literal strings. Second, you can only compare aggregated values with the
HAVINGclause:Also I find it often a lot clearer to use explicit join syntax, instead of
WHERE: