我需要MySQL查询的帮助来检查两个给定日期之间的当前日期并使用if条件。以下是我的查询声明:
$selMesQuery="select Subject, Message, PlainMessage, ARActiveTime, ARTimeZone, ARStartTime, AREndTime, ARID, UserId, DispLogo, ContentType from mytable";
ARActiveTime 0或1 。
ARStartTime,AREndTime将采用日期时间格式例如:2015-03-25 22:15:00
如何使用mysql查询语句执行此操作?
我可以通过执行给定的查询来执行此操作,检查ARActiveTime是0还是1然后获取当前日期,使用php代码检查日期和日期。但是它只能用mysql语句来完成吗?
可以在查询中使用IF条件吗?我不确定。
非常感谢任何帮助。
sample data of mysql table:
_________________________________________________________________________________
table header is as per the mysql query given
__________________________________________________________________________________
sub1 | msg1 | pmsg1 | 0 | 0 |0000-00-00 00:00:00|0000-00-00 00:00:00| 1 | 1 | 0 | 2
_________________________________________________________________________________
sub2 | msg2 | pmsg2 | 1 | 3 |2015-03-12 10:00:00|2015-03-25 22:00:00| 2 | 1 | 1 | 1
_________________________________________________________________________________
sub3 | msg3 | pmsg3 | 1 | 2 |2015-03-10 20:00:00|2015-03-15 20:00:00| 3 | 2 | 1 | 2
_________________________________________________________________________________
sub4 | msg4 | pmsg4 | 0 | 0 |0000-00-00 00:00:00|0000-00-00 00:00:00| 4 | 3 | 0 | 3
_________________________________________________________________________________
desired result:
_________________________________________________________________________________
sub1 | msg1 | pmsg1 | 1 | 1 | 0 | 2
_________________________________________________________________________________
sub2 | msg2 | pmsg2 | 1 | 3 |2015-03-12 10:00:00|2015-03-25 22:00:00| 2 | 1 | 1 | 1
_________________________________________________________________________________
sub3 | msg3 | pmsg3 | 1 | 2 |2015-03-10 20:00:00|2015-03-15 20:00:00| 3 | 2 | 1 | 2
_________________________________________________________________________________
sub4 | msg4 | pmsg4 | 4 | 3 | 0 | 3
_________________________________________________________________________________
答案 0 :(得分:0)
试试这个
SELECT SUBJECT,
Message,
PlainMessage,
ARActiveTime,
ARTimeZone,
ARStartTime,
AREndTime,
ARID,
UserId,
DispLogo,
ContentType FROM mytable WHERE ARActiveTime = 1
UNION ALL
SELECT SUBJECT,
Message,
PlainMessage,
ARActiveTime,
NULL AS ARTimeZone,
NULL AS ARStartTime,
NULL AS AREndTime,
ARID,
UserId,
DispLogo,
ContentType FROM access_mgmt WHERE ARActiveTime = 0;