Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
218 views
in Technique[技术] by (71.8m points)

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...