Mysql查询几天前或几天后的日期

查询 当天±天数 后的日期。“-14”表示14天前的日期,“14”表示14天后的日期 

NOW()精确到时分秒,CURDATE()只精确到天
#查询今天
1.select NOW() as 'time'  
2.select CURDATE() as 'time'   

#查询14天之前
3.select ADDDATE(NOW(),INTERVAL -14 DAY) as 'time'  
4.select ADDDATE(CURDATE(),INTERVAL -14 DAY) as 'time'  

#查询14天之后
5.select ADDDATE(NOW(),INTERVAL 14 DAY) as 'time'  
6.select ADDDATE(CURDATE(),INTERVAL 14 DAY) as 'time' 

#查询指定时间的2天之前
7.select ADDDATE('2023-01-02 10:10:10',INTERVAL -2 DAY) as 'time'
8.select ADDDATE('2023-01-02',INTERVAL -2 DAY) as 'time'

执行结果:
1. 2023-01-02 15:19:18
2. 2023-01-02
3. 2022-12-19 15:19:18
4. 2022-12-19
5. 2023-01-16 15:19:18
6. 2023-01-16
7. 2022-12-31 10:10:10
8. 2022-12-31

 



 

posted @ 2022-12-07 11:11  dabeen  阅读(1542)  评论(0编辑  收藏  举报