收到需求处理一批玩家,在ExpireTime:某一时间上添加30天;ActiveTime:时间设置为当前时间,DailyRewardTime:时间设计为某一个固定时间
image.png#MySQL语句:
UPDATE p_charge_card_info AS p SET p.ExpireTime = DATE_ADD(p.ExpireTime, INTERVAL 30 DAY) ,p.ActiveTime=NOW(),p.DailyRewardTime="2021-01-01 00:00:00"
WHERE `PlayerId` IN ('111','222','333') AND `CardId` = '2';
#关键使用Mysql的Date_ADD()时间函数
Date_ADD()和NOW()
set @dt = now();
select Date_ADD(@dt, interval 1 day); - ->加1天
select Date_ADD(@dt, interval 1 hour); -加1小时
select Date_ADD(@dt, interval 1 minute); - 加1分钟
select Date_ADD(@dt, interval 1 second); -加1秒
select Date_ADD(@dt, interval 1 microsecond);-加1毫秒
select Date_ADD(@dt, interval 1 week);-加1周
select Date_ADD(@dt, interval 1 month);-加1月
select Date_ADD(@dt, interval 1 quarter);-加1季
select Date_ADD(@dt, interval 1 year);-加1年
#关键使用Mysql的Date_Sub()时间函数
select Date_Sub(@dt, interval 1 day); - ->减1天
Mysql时间转化函数(1:string转化为想要时间格式,2:时间格式转化为String)
#string转化为想要时间格式date_format(string,时间格式)
select date_format('2008-08-08 22:23:01', '%Y%m%d%H%i%s'); --输出:20080808222301
#时间格式转化为String str_to_date(str,format)
select str_to_date('08/09/2008', '%m/%d/%Y'); -- 2008-08-09
select str_to_date('08/09/08' , '%m/%d/%y'); -- 2008-08-09
select str_to_date('08.09.2008', '%m.%d.%Y'); -- 2008-08-09
select str_to_date('08:09:30', '%h:%i:%s'); -- 08:09:30
select str_to_date('08.09.2008 08:09:30', '%m.%d.%Y %h:%i:%s'); -- 2008-08-09 08:09:30
#string还可以转化为天 to_days(string)
select to_days('0000-00-00'); -- 0
select to_days('2008-08-08'); -- 733627
#mySQL (时间、秒)转换函数:time_to_sec(time), sec_to_time(seconds)
select time_to_sec('01:00:05'); -- 3605
select sec_to_time(3605); -- '01:00:05'
#MySQL 拼凑日期、时间函数:makdedate(year,dayofyear), maketime(hour,minute,second)
select makedate(2001,31); -- '2001-01-31'
select makedate(2001,32); -- '2001-02-01'
select maketime(12,15,30); -- '12:15:30'
#MySQL (Unix 时间戳、日期)转换函数
unix_timestamp(),
unix_timestamp(date),
from_unixtime(unix_timestamp),
from_unixtime(unix_timestamp,format)
select unix_timestamp(); -- 1218290027
select unix_timestamp('2008-08-08'); -- 1218124800
select unix_timestamp('2008-08-08 12:30:00'); -- 1218169800
select from_unixtime(1218290027); -- '2008-08-09 21:53:47'
select from_unixtime(1218124800); -- '2008-08-08 00:00:00'
select from_unixtime(1218169800); -- '2008-08-08 12:30:00'
select from_unixtime(1218169800, '%Y %D %M %h:%i:%s %x'); -- '2008 8th August 12:30:00 2008'
#MySQL 日期、时间相减函数:datediff(date1,date2), timediff(time1,time2)
select datediff('2008-08-08', '2008-08-01'); -- 7
select timediff('2008-08-08 08:08:08', '2008-08-08 00:00:00'); -- 08:08:08
select timediff('08:08:08', '00:00:00'); -- 08:08:08
#MySQL 时间戳(timestamp)转换、增、减函数:
select timestamp('2008-08-08'); -- 2008-08-08 00:00:00
select timestamp('2008-08-08 08:00:00', '01:01:01'); -- 2008-08-08 09:01:01
select timestamp('2008-08-08 08:00:00', '10 01:01:01'); -- 2008-08-18 09:01:01
select timestampadd(day, 1, '2008-08-08 08:00:00'); -- 2008-08-09 08:00:00
select date_add('2008-08-08 08:00:00', interval 1 day); -- 2008-08-09 08:00:00
#MySQL 时区(timezone)转换函数convert_tz(dt,from_tz,to_tz)
select convert_tz('2008-08-08 12:00:00', '+08:00', '+00:00'); -- 2008-08-08 04:00:00
select date_add('2008-08-08 12:00:00', interval -8 hour); -- 2008-08-08 04:00:00
select date_sub('2008-08-08 12:00:00', interval 8 hour); -- 2008-08-08 04:00:00
select timestampadd(hour, -8, '2008-08-08 12:00:00'); -- 2008-08-08 04:00:00
在mysql中获取时间一般使用new(),但是还有SysDate();SysDate()和New()不同的是SysDate()是动态获取时间的
#比如两个查询语句
SELECT NOW(), SLEEP(3), NOW();
2021-06-11 10:14:55 0 2021-06-11 10:14:55
SELECT SYSDATE(), SLEEP(3), SYSDATE();
2021-06-11 10:13:58 0 2021-06-11 10:14:01
sleep(3)对sysDate()有影响,而对New()没影响
参考:https://www.cnblogs.com/she27/archive/2009/01/16/1377089.html
网友评论