t_community
Create Table |
CREATE TABLE `t_community` ( `id` int(111) NOT NULL AUTO_INCREMENT, `name` varchar(25) COLLATE utf8_bin DEFAULT NULL COMMENT '小区名称', `street` varchar(45) COLLATE utf8_bin DEFAULT NULL COMMENT '街道', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=utf8 COLLATE=utf8_bin |
t_flats
Create Table |
CREATE TABLE `t_room` ( `id` int(11) NOT NULL AUTO_INCREMENT, `room_name` varchar(1) COLLATE utf8_bin DEFAULT NULL COMMENT '房间号ABCD', `flats_id` int(11) DEFAULT NULL COMMENT '公寓id', `status` int(1) DEFAULT NULL COMMENT '出租状态,0未租', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COLLATE=utf8_bin |
t_room
Create Table |
CREATE TABLE `t_room` ( `id` int(11) NOT NULL AUTO_INCREMENT, `room_name` varchar(1) COLLATE utf8_bin DEFAULT NULL COMMENT '房间号ABCD', `flats_id` int(11) DEFAULT NULL COMMENT '公寓id', `status` int(1) DEFAULT NULL COMMENT '出租状态,0未租', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COLLATE=utf8_bin |
结果:
SELECT
TF.flat_num AS "公寓编号",
COUNT(CASE WHEN TR.`status` = 0 THEN TR.id END) AS "未出租数量",
COUNT(TR.id) AS "该公寓所有房间数量",
(CASE WHEN TF.is_private = 1 THEN '是' ELSE '否' END) AS "公寓是否是自营",
CONCAT(TC.`name`, '-', TC.street) AS "小区地址"
FROM
t_flats TF
LEFT JOIN t_community TC ON (TF.community_id = TC.id)
LEFT JOIN t_room TR ON (TR.flats_id = TF.id)
GROUP BY
TF.flat_num;
结果图示: