PHP - Wrong SELECT - Query
I have this code:
$r=mysql_query("SELECT * FROM advertisements WHERE exposure!='0' AND `status`='2' AND clicks_left_micro>0 OR clicks_left_mini>0 OR clicks_left_standard>0 OR clicks_left_extended>0");
The above code, should ONLY get the 2 first rows, in my database. (Please check the pic below): http://i51.tinypic.co开发者_运维问答m/dejiw.png
Why does it select all 3 rows, when I specifically say status="2"?
How can I fix this?
Thanks in advance.
Here is what you have:
SELECT * FROM advertisements
WHERE exposure!='0'
AND `status`='2'
AND clicks_left_micro>0
OR clicks_left_mini>0
OR clicks_left_standard>0
OR clicks_left_extended>0
But I think that this is what you actually wanted:
SELECT * FROM advertisements
WHERE exposure!='0'
AND `status`='2'
AND (clicks_left_micro>0
OR clicks_left_mini>0
OR clicks_left_standard>0
OR clicks_left_extended>0)
You have to use parenthesis to respect boolean operators priority.
Try encapsling the OR part in parentheses
AND clicks_left_micro>0 OR clicks_left_mini>0 OR
clicks_left_standard>0 OR clicks_left_extended>0
changed to
AND (clicks_left_micro>0 OR clicks_left_mini>0 OR
clicks_left_standard>0 OR clicks_left_extended>0)
should solve it.
精彩评论