php - Between date in Mysql not working properly -
i want filter items based on 2 different dates using mysql. in database store data 2017-03-28 10:55:10
. need date part, not time used date()
function:
select sum(cashamount) sumcashsalesamount, date(transactiondate) datepart master_transaction transactiondate between '2017-02-22%' , '2017-03-28%' order transactiondate desc
above query have 2 dates 2017-02-22%
, 2017-03-28%
return no result.
but when change 2017-03-28%
date 2017-03-29%
(tomorrow date) results.
don't use between
"dates". put in quotes, because values datetime values.
the best way write condition is:
where transactiondate >= '2017-02-22' , transactiondate < '2017-03-29'
note inequality second condition. between
inclusive, include midnight on 2017-03-28.
why best?
- it allows query optimizer take advantage of indexes , partitions.
- it logic want.
- it works both
date
,datetime
types.
Comments
Post a Comment