Check out this MySQL
Query and then I\'ll show you what I really want it to do...
mysql_query(\"
SELECT * FROM Drinks WHERE
email=\'$Email\'
Use brackets to group the OR statements.
mysql_query("SELECT * FROM Drinks WHERE email='$Email' AND (date='$Date_Today' OR date='$Date_Yesterday' OR date='$Date_TwoDaysAgo' OR date='$Date_ThreeDaysAgo' OR date='$Date_FourDaysAgo' OR date='$Date_FiveDaysAgo' OR date='$Date_SixDaysAgo' OR date='$Date_SevenDaysAgo')");
You can also use IN
mysql_query("SELECT * FROM Drinks WHERE email='$Email' AND date IN ('$Date_Today','$Date_Yesterday','$Date_TwoDaysAgo','$Date_ThreeDaysAgo','$Date_FourDaysAgo','$Date_FiveDaysAgo','$Date_SixDaysAgo','$Date_SevenDaysAgo')");
Your question is about the operator precedences in mysql and Alex has shown you how to "override" the precedence with parentheses.
But on a side note, if your column date
is of the type Date you can use MySQL's date and time functions to fetch the records of the last seven days, like e.g.
SELECT
*
FROM
Drinks
WHERE
email='$Email'
AND date >= Now()-Interval 7 day
(or maybe Curdate() instead of Now())
Use brackets:
mysql_query("SELECT * FROM Drinks WHERE email='$Email' AND
(date='$Date_Today'
OR date='$Date_Yesterday'
OR date='$Date_TwoDaysAgo'
OR date='$Date_ThreeDaysAgo'
OR date='$Date_FourDaysAgo'
OR date='$Date_FiveDaysAgo'
OR date='$Date_SixDaysAgo'
OR date='$Date_SevenDaysAgo'
)
");
But you should alsos have a look at the IN
operator. So you can say ´date IN ('$date1','$date2',...)`
But if you have always a set of consecutive days why don't you do the following for the date part
date <= $Date_Today AND date >= $Date_SevenDaysAgo
Wrap your AND logic in parenthesis, like this:
mysql_query("SELECT * FROM Drinks WHERE email='$Email' AND (date='$Date_Today' OR date='$Date_Yesterday' OR date='$Date_TwoDaysAgo' OR date='$Date_ThreeDaysAgo' OR date='$Date_FourDaysAgo' OR date='$Date_FiveDaysAgo' OR date='$Date_SixDaysAgo' OR date='$Date_SevenDaysAgo')");
try this
mysql_query("
SELECT * FROM Drinks WHERE
email='$Email'
AND date='$Date_Today'
OR date='$Date_Yesterday', '$Date_TwoDaysAgo', '$Date_ThreeDaysAgo', '$Date_FourDaysAgo', '$Date_FiveDaysAgo', '$Date_SixDaysAgo', '$Date_SevenDaysAgo'"
);
my be like this
OR date='$Date_Yesterday' oR '$Date_TwoDaysAgo'.........