1.这个需求刚开始的时候我是很蒙的,不咋知道咋怎么写刚开始自己想的是这么写的
select dev_code,dryer_id,dry_temp,MAX(begin_time) as begin_time from devices a LEFT JOIN orders_dispatch b ON a.dev_id = b.dryer_id where a.type_id=2 GROUP BY a.dev_code,b.dryer_id,dry_temp
查询结果肯定是这样的肯定不对由于分组的原因吧多余的数据也查询了出来
后来在网上查了点资料在根据自己的修改吧sql改造成了这样子
先把设备编号分组出来加上日期,变成一个表获取到最新时间的设备编号
在根原始表进行联查,通过id和时间进行数据过滤吧数据查询出来。
*[HTML]:
select temp.dev_code,t.dry_temp,t.begin_time from (select dev_code,dryer_id,MAX(begin_time) as begin_time from devices a LEFT JOIN orders_dispatch b ON a.dev_id = b.dryer_id where a.type_id=2 GROUP BY a.dev_code,b.dryer_id) as temp left join orders_dispatch as t on temp.dryer_id=t.dryer_id and temp.begin_time=t.begin_time;
*[HTML]:
上结果,现在查询出来的数据就对啦