假设我有两张桌子,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;