所以我需要帮助来搜索一些信息。我正在寻找房间里有空闲空间的房间。我添加了表格的图片,它在之前和之后的样子。我希望那是足够的信息:)
问问题
31 次
3 回答
0
首先找出每个房间里有多少人:
SELECT door_nr
, Count(*) As number_of_occupants
FROM people
GROUP
BY door_nr
rooms
然后我们可以使用此查询并根据匹配door_nr
值将其连接回表。
SELECT rooms.door_nr
, rooms.space As capacity
, room_usage.number_of_occupants
, rooms.space - room_usage.number_of_occupants As free_space
FROM rooms
LEFT
JOIN (
SELECT door_nr
, Count(*) As number_of_occupants
FROM people
GROUP
BY door_nr
) As room_usage
ON room_usage.door_nr = rooms.door_nr
于 2013-11-13T13:09:42.120 回答
0
SELECT Door_Nr, COUNT(*) FROM People GROUP BY Door_Nr
会给你房间里的人数。
有了(My)SQL的基本工作知识,您应该能够弄清楚其余的吗?否则,请提出具体问题。
于 2013-11-13T13:09:49.760 回答
0
select r.door_nr,
r.space - sum(p.name is not null) as free
from rooms r
left join people p on p.door_nr = r.door_nr
group by r.door_nr, r.space
having free > 0
SQLFiddle 演示
于 2013-11-13T13:09:55.493 回答