MySQL “Or” Condition

后端 未结 5 455
攒了一身酷
攒了一身酷 2020-12-29 21:38

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\'         


        
5条回答
  •  误落风尘
    2020-12-29 22:08

    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
    

提交回复
热议问题