MySQL“或”健康)状况
查看此 MySQL
查询,然后我将向您展示我真正想要它做什么...
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
不等于电子邮件列,也会从查询中选择该日期。
因此,简而言之,我希望电子邮件始终等于电子邮件列,并且如果查询提取的日期等于 $Daye_TwoDaysAgo
或 $Date_ThreeDaysAgo
等,但事实并非如此。不等于电子邮件,则不要提取它。
我想我的查询看起来有点像这样,但我很确定它不会工作..
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'");
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'");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用括号对 OR 语句进行分组。
您还可以使用 IN
Use parentheses to group the OR statements.
You can also use IN
使用方括号:
但是您还应该看看
IN
运算符。所以你可以说 'date IN ('$date1','$date2',...)`但是如果你总是有一组连续的天,为什么不对日期部分执行以下操作
Use brackets:
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
您的问题是关于 mysql 中的 运算符优先级 和Alex 向您展示了如何用括号“覆盖”优先级。
但顺便说一句,如果您的列
date
的类型为日期
您可以使用MySQL 的日期和时间函数 用于获取过去 7 天的记录,例如(或者可能是 Curdate() 而不是 Now())
Your question is about the operator precedences in mysql and Alex has shown you how to "override" the precedence with parentheses.
But on a side note, if your column
date
is of the typeDate
you can use MySQL's date and time functions to fetch the records of the last seven days, like e.g.(or maybe Curdate() instead of Now())
将 AND 逻辑括在括号中,如下所示:
Wrap your AND logic in parenthesis, like this:
试试这个
我是这样的
try this
my be like this