0

我有 3 张桌子,分别称为 Doctor、Appointment 和 Person。我需要将医生的姓名添加到以下结果中:

在此处输入图像描述

使用此代码:

SELECT doctor.doctor_id, COUNT(appointment.appt_time) AS No_APP 
FROM doctor
LEFT JOIN appointment 
ON appointment.doctor_id = doctor.doctor_id
GROUP BY doctor.doctor_id;

我可以对其进行哪些修改,以便我可以使用它的医生姓名,这是我的表格列表(名称在带有医生 ID 的人员表中):

在此处输入图像描述

4

1 回答 1

2
SELECT
    doctor.doctor_id,
    person.last_name,
    COUNT(appointment.appt_time) AS No_APP 
FROM
    doctor
LEFT JOIN
    appointment  ON appointment.doctor_id = doctor.doctor_id
LEFT JOIN
    person  ON doctor.doctor_id = person.person_id
GROUP BY
    doctor.doctor_id,
    person.last_name;

或者你甚至可以跳过doctor表格:

SELECT
    person.person_id,
    person.last_name,
    COUNT(appointment.appt_time) AS No_APP 
FROM
    person
LEFT JOIN
    appointment ON appointment.doctor_id = person.person_id
GROUP BY
    person.person_id,
    person.last_name;
于 2013-03-14T17:43:11.053 回答