MySQL quot;Orquot; Condition(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')");
这篇关于MySQL“或"状况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MySQL“或"状况
基础教程推荐
- Libpuzzle 索引数百万张图片? 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01