Select records where date == now + 21 days (NOT be

2019-09-21 00:34发布

问题:

I have a table session_dates with some fields and a timestamp field named timestart.

What I would like to do is select all the records from my table where the field timestart (TIMESTAMP) is equal to 21 days from now.

Like for example if today is 27 januari -> 17 februari.

I know how I can select all between two dates. My SQL Query for between 2 dates:

SELECT timestart, timefinish, sessionid 
FROM sessions_dates 
WHERE timestart BETWEEN UNIX_TIMESTAMP(NOW()) AND UNIX_TIMESTAMP(DATE_ADD(NOW(), INTERVAL 21 DAY))

But how to select equal to a date?

UPDATE:

I know now that I just have to use the = statement. But how can I test this? How do I know what the UNIX_TIMESTAMP(DATE_ADD(NOW(), INTERVAL 21 DAY)) returns?

回答1:

I think you want:

SELECT timestart, timefinish, sessionid 
FROM sessions_dates 
WHERE timestart >= UNIX_TIMESTAMP(DATE_ADD(NOW(), INTERVAL 21 DAY)) AND
      tmestamp < UNIX_TIMESTAMP(DATE_ADD(NOW(), INTERVAL 22 DAY))

Presumably, timestart has a time component. This version takes that into account and still would allow the use of an index on timestart.