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'");
Description. The MySQL OR Condition is used to test two or more conditions where records are returned when any one of the conditions are met. It can be used in a SELECT, INSERT, UPDATE, or DELETE statement.
The MySQL IN condition is used to help reduce the need to use multiple OR conditions in a SELECT, INSERT, UPDATE, or DELETE statement.
In MySQL, you can use the <> or != operators to test for inequality in a query. For example, we could test for inequality using the <> operator, as follows: SELECT * FROM contacts WHERE last_name <> 'Johnson';
OR Operator: This operator displays the records where either one of the conditions condition1 and condition2 evaluates to True. That is, either condition1 is True or condition2 is True.
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')");
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With