Mysql only updates the date but not the hour, minute and second. Mysql takes the largest data per day within 30 days

================================

©Copyright Sweet Potato Yao 2022-03-10

https://www.cnblogs.com/fanshuyao/

1. mysql only updates the date, not the hours, minutes and seconds

method one:

#mysql只更新日期,不更新时分秒
#mysql只修改日期字段的年月日,不修改时分秒

UPDATE table_name SET create_time=DATE_FORMAT(CONCAT('2022-03-10 ',TIME(create_time)),'%Y-%m-%d %H:%i:%S');

Method two:

#mysql只更新日期,不更新时分秒
#mysql只修改日期字段的年月日,不修改时分秒

UPDATE device_detail l SET l.`create_time`= ADDTIME(DATE('2022-03-10') + INTERVAL 0 HOUR, TIME( l.`create_time`)) WHERE l.`id`=1;

Second, mysql takes the data of the daily maximum value

#取每天最大值的数据

SELECT DATE_FORMAT(create_time, "%Y-%m-%d") create_time, MAX(temperature) temperature, MAX(humidity) humidity, 
MAX(smog) smog, MAX(fan) fan, MAX(acousto_optic) acousto_optic
FROM device_detail 
WHERE DATE_FORMAT(create_time, "%Y-%m-%d")='2022-03-07'
GROUP BY DATE_FORMAT(create_time, "%Y-%m-%d");

3. MySQL fetches data within 30 days

#取30天内的数据

SELECT * FROM device_detail
WHERE DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= DATE(create_time);

Fourth, mysql takes the largest data per day in 30 days

#取30天内每天最大的数据

SELECT DATE_FORMAT(create_time, "%Y-%m-%d") create_time, MAX(temperature) temperature, MAX(humidity) humidity, 
MAX(smog) smog, MAX(fan) fan, MAX(acousto_optic) acousto_optic
FROM (
SELECT * FROM device_detail
WHERE DATE_SUB(CURDATE(), INTERVAL 30 DAY) <= DATE(create_time)
) t
GROUP BY DATE_FORMAT(create_time, "%Y-%m-%d");

(Time is precious, sharing is not easy, donate and give back, ^_^)

 

================================

©Copyright Sweet Potato Yao 2022-03-10

https://www.cnblogs.com/fanshuyao/

Guess you like

Origin blog.csdn.net/w995223851/article/details/123415533