Mysql 语句

查询一天中 每个小时的数据

SELECT timetable.hour Hour, ifnull(sumtable.room_temperature, 0) room_temperature,ifnull(sumtable.room_humidity, 0) room_humidity, ifnull(sumtable.co2_concentration, 0) co2_concentration, ifnull(sumtable.illumination_intensity, 0) illumination_intensity,ifnull(sumtable.soil_temperature, 0) soil_temperature,ifnull(sumtable.soil_humidity, 0) soil_humidity FROM (SELECT 0 hour UNION ALL SELECT 2 hour UNION ALL SELECT 4 hour UNION ALL SELECT 6 hour UNION ALL SELECT 8 hour UNION ALL SELECT 10 hour UNION ALL SELECT 12 hour UNION ALL SELECT 14 hour UNION ALL SELECT 16 hour UNION ALL SELECT 18 hour UNION ALL SELECT 20 hour UNION ALL SELECT 22 hour UNION ALL SELECT 24 hour) timetable LEFT JOIN( SELECT
hour(A.created_date) hour,
A.room_temperature,
A.room_humidity,
A.co2_concentration,
A.illumination_intensity,
A.soil_temperature,
A.soil_humidity
FROM iot_greenhouse_realtime_data AS A
WHERE ABS(DATEDIFF(now(),A.created_date)) <=1 and (DATEDIFF(now(),A.created_date)) >0 AND greenhouse_code=‘A1’
GROUP BY date_format(A.created_date, ‘%Y%m%d-%H’), hour
) sumtable ON timetable.hour = sumtable.hour ORDER BY hour;

改变查询到的内容

SELECT case when DATE_FORMAT(A.created_date,’%Y-%m-%d’)=DATE_SUB(curdate(),INTERVAL 1 DAY) then ‘昨天’ when DATE_FORMAT(A.created_date,’%Y-%m-%d’)=DATE_SUB(curdate(),INTERVAL 2 DAY) then ‘前天’ else DATE_FORMAT(A.created_date,’%m-%d’) end weight, SUM(A.weight) AS weight FROM iot_fertilization_log AS A
WHERE ABS(DATEDIFF(now(),A.created_date)) <=7 and (DATEDIFF(now(),A.created_date)) >0 and A.greenhouse_code=‘A1’
GROUP BY DATE_FORMAT(A.created_date,’%Y-%m-%d’)

mysql 获得最新的数据并且去除重复

商品表 product

字段 id、 product_code、product_name、create_date

select * from (select *,product_code as code from product order by create_date desc) t group by code;

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值