我有3个表,一个名为Doctor,Person,一个名为Appointment。他们有一个共同关系的doctor_id,但只有一个人拥有他们的Last_Name。
我需要能够生成一份医生ID列表,以及为每位医生预约的次数,无论是预约还是医生,还有医生姓名。
我如何实施医生的名字?
以下是表格:
这是我到目前为止所做的:
SELECT doctor.doctor_id, COUNT(appt_time) AS No_APP
FROM doctor LEFT JOIN appointment ON doctor.doctor_id = appointment.doctor_id
GROUP BY doctor.doctor_id;
答案 0 :(得分:1)
只需为新表添加一个左连接:
SELECT doctor.doctor_id, COUNT(appt_time) AS No_APP, person.Last_name
FROM doctor LEFT JOIN appointment ON doctor.doctor_id = appointment.doctor_id
LEFT JOIN person on person.doctor_id = doctor.doctor_id
GROUP BY doctor.doctor_id;
答案 1 :(得分:0)
假设人员表包含指向医生的链接(如果需要,将在第三个表后发布时更新)
SELECT d.doctor_id, p.last_name, COUNT(a.appt_time) AS No_APP
FROM doctor d
LEFT JOIN appointment a ON d.doctor_id = a.doctor_id
LEFT JOIN person p ON d.doctor_id = p.doctor_id
GROUP BY d.doctor_id;