Select records where date == now + 21 days (NOT between)

穿精又带淫゛_ 提交于 2019-12-14 03:24:32

问题


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.



来源:https://stackoverflow.com/questions/28176670/select-records-where-date-now-21-days-not-between

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!