数据表如下:

统计总数

SELECT h.*,IFNULL(count(r.pid),0) AS 总数
FROM rts_room AS r RIGHT JOIN rts_house AS h
ON r.pid = h.Id
AND r.state != 3
WHERE h.state = 0
GROUP BY h.Id

输出结果如下:

统计总数及闲置的个数

SELECT 
h.*,
SUM(CASE r.state WHEN "0" THEN 1 ELSE 0 END)+
SUM(CASE r.state WHEN "1" THEN 1 ELSE 0 END) AS 总数,
SUM(CASE r.state WHEN "0" THEN 1 ELSE 0 END) AS 闲置
FROM rts_room AS r RIGHT JOIN rts_house AS h
ON r.pid = h.Id
WHERE h.state = 0
GROUP BY h.Id

输出结果如下:

 

版权声明:本文为clqbolg原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/clqbolg/p/11376095.html