简体   繁体   中英

MySQL: Who are today “on” between 2 different dates

I have a Mysql problem. (I use PHP with it).

For example I have this kind of database:

Name | Started and | will end
Jason | 2009-12-17 | 2009-12-24
Ericke | 2009-12-12 | 2009-12-30
Maria | 2010-01-02 | 2010-01-10

With mysql (or php) question I would like to get the names of people, which are "on" right now (2009-12-19).

The answer should be:
Jason
Ericke

Thank you for your great help in advance!

what about a query in which you compare dates ?

Something like this, I suppose, should do :

select name
from your_table
where started <= curdate() 
    and will_end >= curdate()

Notes :

  • You'll need to adjust the table and columns names, of course
  • and maybe you'll want to use > and < instead of >= and <= ; depends on your data.
  • I use the curdate() function, which gets the date of the current day -- I like it better than injecting the date into the query using some PHP code.
SELECT *
  FROM `table`
 WHERE CURDATE() BETWEEN `startdate` AND `enddate`

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