MySQL“或"状况

2022-01-20 00:00:00 select conditional-statements php mysql

查看这个 MySQL 查询,然后我会告诉你我真正想要它做什么......

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

它的问题是我希望它始终与电子邮件匹配.在这种情况下(例如),如果日期等于 $Date_SixDaysAgo,那么即使 $Email 不等于电子邮件列,也会从查询中选择它.

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.

所以,简而言之,我希望电子邮件始终等于电子邮件列,并且如果查询提取的日期等于 $Daye_TwoDaysAgo$Date_ThreeDaysAgo 等.但不等于电子邮件,然后不要拉它.

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

推荐答案

使用括号对 OR 语句进行分组.

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

你也可以使用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')");

相关文章