Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL "Or" Condition

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'"); 
like image 985
Nick Chubb Avatar asked Dec 22 '11 13:12

Nick Chubb


People also ask

Is There A OR condition in MySQL?

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.

What is condition in MySQL?

The MySQL IN condition is used to help reduce the need to use multiple OR conditions in a SELECT, INSERT, UPDATE, or DELETE statement.

Can I use != In MySQL?

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

What does OR condition mean in SQL?

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.


Video Answer


2 Answers

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')"); 
like image 130
Maxim Krizhanovsky Avatar answered Sep 29 '22 03:09

Maxim Krizhanovsky


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 
like image 31
Alex Avatar answered Sep 29 '22 01:09

Alex