如何检查MySQL中两个给定日期之间的当前日期?

时间:2015-03-13 11:18:06

标签: php mysql if-statement case

我需要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

  1. 如果 ARActiveTime是&#39; 0&#39; ,我不需要 ARTimeZone,ARStartTime,AREndTime <的值/ em> 但我需要其他值。
  2. 仅当 ARActiveTime为&#39; 1&#39;时,我才需要 ARTimeZone,ARStartTime,AREndTime 的值以及其他值系统时间应介于ARStartTime和AREndTime之间。
  3. 如何使用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
    _________________________________________________________________________________
    

1 个答案:

答案 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;