我有一个名为Rooms的表,看起来像这样
id name room number
-------------------------
1 Dave Room 100
2 NULL ROOM 101
3 Scott Room 100
我需要检查哪个房间是空的,哪个房间有人关联,所以结果将是:
Room name Count
-----------------
Room 100 2 -- Because Dave and Scott is in the room
Room 101 0 -- No name is associated with the room
我刚开始学习SQL,所以我的知识有限我能看到的是
SELECT Count(room_number)
FROM rooms
WHERE name != NULL
...将根据room_number在列表中显示的次数返回2和0,并且与之关联的名称不是0并且
SELECT DISTINCT(room_number)
FROM rooms
...将返回100室和101室
如何组合这两个查询以获得我想要的结果表?
答案 0 :(得分:4)
SELECT RoomNumber, COUNT(Name)
FROM rooms
GROUP BY RoomNumber
我建议添加Room
表并将RoomNumber
更改为外键关系。标准化该列。
答案 1 :(得分:0)
SELECT COUNT(NAME), RoomNumber
FROM ROOMS
GROUP BY RoomNumber