假设我有两张桌子,patients
并且rooms
,并且patients
是
CREATE TABLE patient (
id int,
room int,
FOREIGN KEY (room) REFERENCES room (id)
);
并且room
是
CREATE TABLE rooms (
id int,
);
我想创建一个视图,rooms
其中包括那个房间里有多少病人。我可以计算房间里的病人数量
select count(1) from patients where room = N;
对于任何现有的房间N
。我将如何写SELECT
我需要的语句?
我最好的解决方案:
select *,
count(1) as patients_in_room
from patients
where patients.room = rooms.id
from rooms;