说我有两个表,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;