简体   繁体   中英

How to check current date between two given dates in MySQL?

I need help with a MySQL query to check current date between two given dates and using if condition. Below is my query statement:

$selMesQuery="select Subject, Message, PlainMessage, ARActiveTime, ARTimeZone, ARStartTime, AREndTime, ARID, UserId, DispLogo, ContentType from mytable";

ARActiveTime will be 0 or 1 .

ARStartTime, AREndTime will be in datetime format ex:2015-03-25 22:15:00

  1. If ARActiveTime is '0' , I don't need values of ARTimeZone, ARStartTime, AREndTime but I need other values.
  2. I need the values of ARTimeZone, ARStartTime, AREndTime along with other values only if ARActiveTime is '1' and system time should be in between ARStartTime and AREndTime.

How do I do this with mysql query statements??

I can do this by executing the given query, checking whether ARActiveTime is 0 or 1 and then get current date, checking from and to dates with php code. But can it be done only with mysql statements only?

May be can it be done using IF condition in query? I'm not sure.

Any help would be greatly appreciated.

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
_________________________________________________________________________________

try this

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM