MySQL更新时间,加上减去一段时间

MySQL时间加减函数

DATE_ADD(date,INTERVAL expr type)  往当前日期添加指定时间间隔 

DATE_SUB(date,INTERVAL expr type)  往当前日期减去指定时间间隔 

 

参数解析:

date: 数据表时间字段

expr: 添加/减去的时间间隔

type: 添加/减去的时间单位,可选值如下
MICROSECOND 微秒,一秒等于一百万微秒
SECOND
MINUTE
HOUR
DAY
WEEK
MONTH
QUARTER 季度(3个月)
YEAR
SECOND_MICROSECOND 输入微秒转换为秒, 1000000微秒等于1秒
MINUTE_MICROSECOND 输入微秒转换为分, 60*1000000微秒等于一分钟
MINUTE_SECOND 输入秒转换为分, 80秒等于1分20秒
HOUR_MICROSECOND 输入微秒转换为小时, 1000000*60*60微秒等于一小时
HOUR_SECOND 输入秒转换为小时
HOUR_MINUTE 输入分钟转换为小时
DAY_MICROSECOND 输入微秒转换为天
DAY_SECOND 输入秒转换为天
DAY_MINUTE 输入分转换为天
DAY_HOUR 输入时转换为天
YEAR_MONTH 输入月转换为年
vip表, 字段id,overdue_time过期时间

数据初始值, 一条记录, id = 1, overdue_time = 2019-08-20 00:00:00


添加时间
DATE_ADD()


1: MICROSECOND

UPDATE vip SET overdue_time = DATE_ADD(overdue_time,INTERVAL 1000000 MICROSECOND)

添加一百万微秒后: 2019-08-20 00:00:01

---------------------------------------------------------------------------------

2: SECOND

UPDATE vip SET overdue_time = DATE_ADD(overdue_time,INTERVAL 1 SECOND) 

添加一秒后: 2019-08-20 00:00:02

---------------------------------------------------------------------------------

3: MINUTE   

UPDATE vip SET overdue_time = DATE_ADD(overdue_time,INTERVAL 1 MINUTE) 

添加一分钟后: 2019-08-20 00:01:02

---------------------------------------------------------------------------------

4: HOUR   

UPDATE vip SET overdue_time = DATE_ADD(overdue_time,INTERVAL 1 HOUR) 

添加一小时后: 2019-08-20 01:01:02

---------------------------------------------------------------------------------

5: DAY

UPDATE vip SET overdue_time = DATE_ADD(overdue_time,INTERVAL 1 DAY)

添加一天后: 2019-08-21 01:01:02

---------------------------------------------------------------------------------

6: WEEK

UPDATE vip SET overdue_time = DATE_ADD(overdue_time,INTERVAL 1 WEEK)

添加一周后: 2019-08-28 01:01:02

---------------------------------------------------------------------------------

7: MONTH

UPDATE vip SET overdue_time = DATE_ADD(overdue_time,INTERVAL 1 MONTH)

添加一个月后: 2019-09-28 01:01:02

---------------------------------------------------------------------------------

8: QUARTER

UPDATE vip SET overdue_time = DATE_ADD(overdue_time,INTERVAL 1 QUARTER)

添加一个季度后: 2019-12-28 01:01:02

---------------------------------------------------------------------------------

9: YEAR

UPDATE vip SET overdue_time = DATE_ADD(overdue_time,INTERVAL 1 YEAR)

添加一年后: 2020-12-28 01:01:02

---------------------------------------------------------------------------------

10: SECOND_MICROSECOND

UPDATE vip SET overdue_time = DATE_ADD(overdue_time,INTERVAL 2000000 SECOND_MICROSECOND) 

添加两百万微秒后: 2020-12-28 01:01:04, 两百万微秒相当于两秒

---------------------------------------------------------------------------------

11: MINUTE_MICROSECOND

UPDATE vip SET overdue_time = DATE_ADD(overdue_time,INTERVAL 1000000 * 60 MINUTE_MICROSECOND) 

添加1000000*60微秒后: 2020-12-28 01:02:04, 1000000*60微秒相当于一分钟

---------------------------------------------------------------------------------

12: MINUTE_SECOND

UPDATE vip SET overdue_time = DATE_ADD(overdue_time,INTERVAL 80 MINUTE_SECOND)

添加80秒后: 2020-12-28 01:03:24, 80 MINUTE_SECOND 相当于1分20秒

---------------------------------------------------------------------------------

13: HOUR_MICROSECOND

UPDATE vip SET overdue_time = DATE_ADD(overdue_time,INTERVAL 1000000 * 60 * 60 HOUR_MICROSECOND)

添加1000000*60*60微秒后: 2020-12-28 02:03:24, 1000000*60*60微秒相当于1小时

---------------------------------------------------------------------------------

14: HOUR_SECOND

UPDATE vip SET overdue_time = DATE_ADD(overdue_time,INTERVAL 3660 HOUR_SECOND)

添加3660秒后: 2020-12-28 03:04:24, 3660秒相当于1小时1分钟

---------------------------------------------------------------------------------

15: HOUR_MINUTE

UPDATE vip SET overdue_time = DATE_ADD(overdue_time,INTERVAL 66 HOUR_MINUTE)

添加66分钟后: 2020-12-28 04:10:24, 66分钟相当于1小时6分钟

---------------------------------------------------------------------------------

16: DAY_MICROSECOND

UPDATE vip SET overdue_time = DATE_ADD(overdue_time,INTERVAL 1000000 * 60 * 60 * 24 DAY_MICROSECOND)

添加1000000*60*60*24微秒后: 2020-12-29 04:10:24, 1000000*60*60*24微秒相当于1天

---------------------------------------------------------------------------------

17: DAY_SECOND

UPDATE vip SET overdue_time = DATE_ADD(overdue_time,INTERVAL 3600 * 12 + 6 DAY_SECOND)

添加3600*12+6秒后: 2020-12-29 16:10:30, 3600*12+6秒相当于12小时6秒

---------------------------------------------------------------------------------

18: DAY_MINUTE

UPDATE vip SET overdue_time = DATE_ADD(overdue_time,INTERVAL 60 * 24 DAY_MINUTE)

添加60*24分钟后: 2020-12-30 16:10:30, 60*24分钟相当于1天

---------------------------------------------------------------------------------

19: DAY_HOUR

UPDATE vip SET overdue_time = DATE_ADD(overdue_time,INTERVAL 28 DAY_HOUR)

添加28小时后: 2020-12-31 20:10:30, 28小时相当于1天4小时

---------------------------------------------------------------------------------

20: YEAR_MONTH

UPDATE vip SET overdue_time = DATE_ADD(overdue_time,INTERVAL 15 YEAR_MONTH)

添加15个月后: 2022-03-31 20:10:30, 15个月相当于1年3个月


vip表, 字段id,overdue_time过期时间

数据初始值, 一条记录, id = 1, overdue_time = 2022-03-31 20:10:30


减去时间
DATE_SUB()


1: MICROSECOND

UPDATE vip SET overdue_time = DATE_SUB(overdue_time,INTERVAL 1000000 MICROSECOND)

减去一百万微秒后: 2022-03-31 20:10:29

---------------------------------------------------------------------------------

2: SECOND

UPDATE vip SET overdue_time = DATE_SUB(overdue_time,INTERVAL 1 SECOND) 

减去一秒后: 2022-03-31 20:10:28

---------------------------------------------------------------------------------

3: MINUTE   

UPDATE vip SET overdue_time = DATE_SUB(overdue_time,INTERVAL 1 MINUTE) 

减去一分钟后: 2022-03-31 20:09:28

---------------------------------------------------------------------------------

4: HOUR   

UPDATE vip SET overdue_time = DATE_SUB(overdue_time,INTERVAL 1 HOUR) 

减去一小时后: 2022-03-31 19:09:28

---------------------------------------------------------------------------------

5: DAY

UPDATE vip SET overdue_time = DATE_SUB(overdue_time,INTERVAL 1 DAY)

减去一天后: 2022-03-30 19:09:28

---------------------------------------------------------------------------------

6: WEEK

UPDATE vip SET overdue_time = DATE_SUB(overdue_time,INTERVAL 1 WEEK)

减去一周后: 2022-03-23 19:09:28

---------------------------------------------------------------------------------

7: MONTH

UPDATE vip SET overdue_time = DATE_SUB(overdue_time,INTERVAL 1 MONTH)

减去一个月后: 2022-02-23 19:09:28

---------------------------------------------------------------------------------

8: QUARTER

UPDATE vip SET overdue_time = DATE_SUB(overdue_time,INTERVAL 1 QUARTER)

减去一个季度后: 2021-11-23 19:09:28

---------------------------------------------------------------------------------

9: YEAR

UPDATE vip SET overdue_time = DATE_SUB(overdue_time,INTERVAL 1 YEAR)

减去一年后: 2020-11-23 19:09:28

---------------------------------------------------------------------------------

10: SECOND_MICROSECOND

UPDATE vip SET overdue_time = DATE_SUB(overdue_time,INTERVAL 2000000 SECOND_MICROSECOND) 

减去两百万微秒后: 2020-11-23 19:09:26, 两百万微秒相当于两秒

---------------------------------------------------------------------------------

11: MINUTE_MICROSECOND

UPDATE vip SET overdue_time = DATE_SUB(overdue_time,INTERVAL 1000000 * 60 MINUTE_MICROSECOND) 

减去1000000*60微秒后: 2020-11-23 19:08:26, 1000000*60微秒相当于一分钟

---------------------------------------------------------------------------------

12: MINUTE_SECOND

UPDATE vip SET overdue_time = DATE_SUB(overdue_time,INTERVAL 80 MINUTE_SECOND)

减去80秒后: 2020-11-23 19:07:06, 80 MINUTE_SECOND 相当于1分20秒

---------------------------------------------------------------------------------

13: HOUR_MICROSECOND

UPDATE vip SET overdue_time = DATE_SUB(overdue_time,INTERVAL 1000000 * 60 * 60 HOUR_MICROSECOND)

减去1000000*60*60微秒后: 2020-11-23 18:07:06, 1000000*60*60微秒相当于1小时

---------------------------------------------------------------------------------

14: HOUR_SECOND

UPDATE vip SET overdue_time = DATE_SUB(overdue_time,INTERVAL 3660 HOUR_SECOND)

减去3660秒后: 2020-11-23 17:06:06, 3660秒相当于1小时1分钟

---------------------------------------------------------------------------------

15: HOUR_MINUTE

UPDATE vip SET overdue_time = DATE_SUB(overdue_time,INTERVAL 66 HOUR_MINUTE)

减去66分钟后: 2020-11-23 16:00:06, 66分钟相当于1小时6分钟

---------------------------------------------------------------------------------

16: DAY_MICROSECOND

UPDATE vip SET overdue_time = DATE_SUB(overdue_time,INTERVAL 1000000 * 60 * 60 * 24 DAY_MICROSECOND)

减去1000000*60*60*24微秒后: 2020-11-22 16:00:06, 1000000*60*60*24微秒相当于1天

---------------------------------------------------------------------------------

17: DAY_SECOND

UPDATE vip SET overdue_time = DATE_SUB(overdue_time,INTERVAL 3600 * 12 + 6 DAY_SECOND)

减去3600*12+6秒后: 2020-11-22 04:00:00, 3600*12+6秒相当于12小时6秒

---------------------------------------------------------------------------------

18: DAY_MINUTE

UPDATE vip SET overdue_time = DATE_SUB(overdue_time,INTERVAL 60 * 24 DAY_MINUTE)

减去60*24分钟后: 2020-11-21 04:00:00, 60*24分钟相当于1天

---------------------------------------------------------------------------------

19: DAY_HOUR

UPDATE vip SET overdue_time = DATE_SUB(overdue_time,INTERVAL 28 DAY_HOUR)

减去28小时后: 2020-11-20 00:00:00, 28小时相当于1天4小时

---------------------------------------------------------------------------------

20: YEAR_MONTH

UPDATE vip SET overdue_time = DATE_SUB(overdue_time,INTERVAL 15 YEAR_MONTH)

减去15个月后: 2019-08-20 00:00:00, 15个月相当于1年3个月


你可能感兴趣的:(mysql数据库)