简体   繁体   中英

PHP & MySQL query date time stamp between

I am attempting to query for a record. The date is stored as a date time stamp and my query looks like this:

SELECT    count(c.id) as totalOrders
FROM      Cart c
WHERE     c.artist_id = 1
AND       c.paid = 1
AND       date_format(c.created, 'Y-m-d') between '2016-09-06' AND '2016-09-07'

My date time stamp is this: 2016-09-07 21:04:46

For some reason this does not return any records, why?

Change the following line:

AND date_format(c.created, 'Y-m-d') between '2016-09-06' AND '2016-09-07'

to

AND date(created) between '2016-09-06' AND '2016-09-07'

date() function will return the date from datetime and it will check in the given range.

use date() function instead of date_format()

SELECT    count(c.id) as totalOrders
    FROM      Cart c
    WHERE     c.artist_id = 1
    AND       c.paid = 1
    AND       date(c.created) between '2016-09-06' AND '2016-09-07'

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