1. 程式人生 > >時間篩選查詢小於加一天 mysql 中date_add 函式

時間篩選查詢小於加一天 mysql 中date_add 函式

SELECT
y.totalIntegral totalIntegral,
y.availableIntegral AS availableIntegral,
y.CreateDate as CreateDate
FROM
(
SELECT
a.Money AS totalIntegral ,a.UseableMoney AS availableIntegral,
w.CreateDate as CreateDate


FROM
account a
LEFT JOIN wateraccount w ON a.id = w.AccountId
WHERE
1=1
-- and a.UserName='18601546682'
and a.UserType='承運商'


and date_format(w.CreateDate,'%Y-%m-%d %H:%i:%s') >=date_format('2018-06-10','%Y-%m-%d %H:%i:%s')
 and date_format(w.CreateDate,'%Y-%m-%d %H:%i:%s')< date_format('2018-06-21','%Y-%m-%d %H:%i:%s')+1


 -- and date_format(w.CreateDate,'%Y-%m-%d %H:%i:%s')< DATE_ADD(date_format('2018-06-21','%Y-%m-%d %H:%i:%s'),  interval 1 day)
-- and w.FromMethod='SEND_PRICE_SALE_SCORE'
-- and w.FromApp='承運商APP'
and w.WaterType='IN'


GROUP BY
a.id
ORDER BY w.ModifyDate DESC

) AS y

解析----------------------------------------

date_add 函式用法

DATE_ADD (interval 1 day)


1.加一相當於小於21和等於21的資料查出來

 and date_format(w.CreateDate,'%Y-%m-%d %H:%i:%s')< date_format('2018-06-21','%Y-%m-%d %H:%i:%s')+1

2.不加一隻查詢小於21的資料查出來等於21的查不出來

 and date_format(w.CreateDate,'%Y-%m-%d %H:%i:%s')< date_format('2018-06-21','%Y-%m-%d %H:%i:%s')

3.相當於小於21和等於21的資料查出來, 與1.相同

-- and date_format(w.CreateDate,'%Y-%m-%d %H:%i:%s')< DATE_ADD(date_format('2018-06-21','%Y-%m-%d %H:%i:%s'),  interval 1 day)

4、小於等於是只查詢小於21的 =相當沒用

 and date_format(w.CreateDate,'%Y-%m-%d %H:%i:%s')<= date_format('2018-06-21','%Y-%m-%d %H:%i:%s')

5、可以在括號後面加一 與1.相同

 and date_format(w.CreateDate,'%Y-%m-%d %H:%i:%s')<= date_format('2018-06-21','%Y-%m-%d %H:%i:%s')+1