0

我有一个名为 person 的表,它有一个名为 person_id 的主键。person 表包含有关患者和医生的信息(姓名首字母、姓氏等)。我还有另一个名为约会的表,其中包含医生 ID、患者 ID 和有关约会的信息。Doctor_id 和 Patient_id 链接到 person 表中的 person_id。

我需要进行一个查询,该查询将返回医生的姓名(使用带有首字母和姓氏的 concat)和患者的姓名。

我试过使用嵌套的选择语句,但没有得到任何地方..

任何信息都会很棒!

4

3 回答 3

3

在 SQL Server 中,连接是通过使用+运算符完成的。所以连接运算符取决于您使用的 RDBMS。(MYSQL,使用CONCAT运算符

基本上,您需要在 tableperson上连接 table 两次,appointment因为有两列依赖于 table person

SELECT  a.*
        doc.firstName + ' ' + doc.lastName as DoctorName,
        pat.firstName + ' ' + pat.lastName as PatientName
FROM    appointment a
        INNER JOIN person doc
            ON a.person_ID = doc.doctor_ID
        INNER JOIN person pat
            ON a.person_ID = pat.patient_ID

要进一步了解有关联接的更多信息,请访问以下链接:

于 2013-03-13T16:14:12.010 回答
3
SELECT d.initials + ' ' + d.last_name AS 'doctor name', p. initials + ' ' + p.last_name AS 'patient name'
FROM Person p
INNER JOIN appointment a
ON p.person_id = a.patient_id
INNER JOIN Person d
ON d.person_id = a.doctor_id
于 2013-03-13T16:14:23.623 回答
0

从这里开始:

select 
  patient.person_id, 
  doctor.person_id
from person as patient
left join appointment 
    on appointment.patient_id = patient.person_id
left join person as doctor 
    on doctor.person_id = appointment.doctor_id 
   and doctor.person_id = patient.doctor_id
于 2013-03-13T16:15:15.610 回答