我就废话不多说了,大家还是直接看代码吧~
select project_no,
sum(case when device_state=0 then 1 else 0 end)as offtotal ,
sum(case when device_state=1 then 1 else 0 end)as onlinetotal,
sum(1)total
from iot_d_device
group by project_no
order by project_no
补充:mysql一条sql语句查询多条统计结果
商城项目难免会遇到用户个人中心页查询不同状态订单数量的问题。当然这个问题并不难,可以写一个dao层方法,以状态作为入参,每次传入不同状态值依次查询相应状态的订单数量。
今天在写h5端接口时,我想换种方式查,也就是通过一条sql查询出多个状态的订单数量。在网上搜了搜,方法可行,所以就尝试了下,果不其然成功了。
示例如下(数据只为演示今天的问题,表设计并不严谨。勿怪):
set foreign_key_checks=0;
-- ----------------------------
-- table structure for mini_test_order
-- ----------------------------
drop table if exists `mini_test_order`;
create table `mini_test_order` (
`id` int(11) not null,
`o