我有1个内连接表
SELECT COUNT(*) as dirtyRoom FROM t_room INNER JOIN t_room_type ON t_room.roomType = t_room_type.code WHERE status = 'sPVDirty' output : dirtyRoom : 13
SELECT COUNT(*) as readyRoom FROM t_room INNER JOIN t_room_type ON t_room.roomType = t_room_type.code WHERE status = 'sPVReady' output : readyRoom : 33
SELECT COUNT(*) as maintenanceRoom FROM t_room INNER JOIN t_room_type ON t_room.roomType = t_room_type.code WHERE status = 'sPVMaint' output : maintenanceRoom : 4
我想将不同条件的每个计数值分组到1个表中,其中3列显示每个值
输出 表1
dirtyRoom readyRoom maintenanceRoom
13 33 4
怎么做?
答案 0 :(得分:0)
试试这个
SELECT COUNT(IF(status='sPVDirty',status,0)) As DirtyRoom,
COUNT(IF(status='sPVReady',status,0)) As ReadyRoom,
COUNT(IF(status='sPVMaint',status,0)) As MaintenanceRoom
FROM t_room INNER JOIN t_room_type ON t_room.roomType = t_room_type.code
或者:
SELECT SUM(IF(status='sPVDirty',1,0)) As DirtyRoom,
SUM(IF(status='sPVReady',1,0)) As ReadyRoom,
SUM(IF(status='sPVMaint',1,0)) As MaintenanceRoom
FROM t_room INNER JOIN t_room_type ON t_room.roomType = t_room_type.code
所以你的输出将是
+------------+------------+--------------------+
| DirtyRoom | ReadyRoom |MaintenanceRoom |
+------------+------------+--------------------+
| 13 | 33 |4 |
+------------+------------+--------------------+
答案 1 :(得分:0)
尝试
SELECT
IF(status = 'sPVDirty', count(status), 0) as dirtyRoom,
IF(status = 'sPVReady', count(status), 0) as readyRoom,
IF(status = 'sPVMaint', count(status), 0) as maintenanceRoom,
FROM t_room INNER JOIN t_room_type ON t_room.roomType = t_room_type.code