如何使用 SQL 显示患者 ID、名字、姓氏和主治医生的专业?
显示患者 ID、名字、姓氏和主治医生的姓名 专业。
仅显示诊断为“癫痫”的患者以及 医生的名字是“丽莎”
检查患者、入院和医生表以了解所需信息 信息。
SELECT a.first_name ,a.last_name,a.patient_id FROM patients a
JOIN admissions AS n ON a.patient_id= n.patient_id
WHERE n.diagnosis='Epilepsy'
UNION
SELECT specialty
FROM admissions AS n
JOIN physicians AS p ON n.attending_physician_id=p.physician_id
WHERE p.first_name='Lisa'
ORDER BY RAND()
LIMIT 6;
病人表
patient_id INT
first_name TEXT
last_name TEXT
gender CHAR(1)
birth_date DATE
city TEXT
primary key icon province_id CHAR(2)
allergies TEXT
height INT
weight INT
入场表
patient_id INT
admission_date DATE
discharge_date DATE
diagnosis TEXT
primary key icon attending_physician_id INT
物理素表
physician_id INT
first_name TEXT
last_name TEXT
specialty TEXT
这是一个解决方案。您还应该查看数据的显示方式(混合大小写)甚至长描述(例如 Epilepsy ABC)。
select i.patient_id, i.first_name, i.last_name,
coalesce(p.specialty,'not provided') as physician_specialty --coalesce in case you decide to left join at some point
from patients i
join admission a
on i.patient_id = a.patient_id
join physicians p
on a.attending_physician_id = p.physician_id
where upper(a.diagnosis) like '%EPILEPSY%'
and upper(p.first_name) = 'LISA'
选择 p.患者 ID、p.名字、p.姓氏、d.专业 来自 AS p 患者 加入招生 ON p.患者_id =a.患者_id 加入医生 AS d ON a.attending_doctor_id =d.doctor_id 其中 a.diagnosis ='癫痫' AND d.first_name ='Lisa'
--试试这个,对我有用。
SELECT
a.patient_id,
a.first_name,
a.last_name,
b.specialty
FROM
patients a,
physicians b,
admission c
WHERE
a.patient_id = c.patient_id
AND c.attending_physician_id = b.physician_id
AND c.diagnosis = 'Epilepsy'
AND b.first_name = 'Lisa';