MySQL "Or" Condition

108,496

Solution 1

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')");

Solution 2

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

Solution 3

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())

Share:
108,496
Nick Chubb
Author by

Nick Chubb

GO DAWGS

Updated on September 28, 2020

Comments

  • Nick Chubb
    Nick Chubb over 3 years

    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'
        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'");
    

    The problem with it is that I want it to always match the email. In this case (for example) if the date equals $Date_SixDaysAgo then it will be selected from the query even if $Email doesn't equal the email column.

    So, in short, I want the email to always equal the email column, and if the query pulls a date that equals $Daye_TwoDaysAgo or $Date_ThreeDaysAgo etc.. but doesn't equal the email then don't pull it.

    I guess my query would look kind of like this, but I'm pretty sure it won't work..

    mysql_query("
        SELECT * FROM Drinks WHERE
        email='$Email'
        AND date='$Date_Today
        || $Date_Yesterday
        || $Date_TwoDaysAgo
        || $Date_ThreeDaysAgo
        || $Date_FourDaysAgo
        || $Date_FiveDaysAgo
        || $Date_SixDaysAgo
        || $Date_SevenDaysAgo'");