查询连接多个表

时间:2016-05-11 13:55:09

标签: mysql sql database

我刚开始使用sql数据库我有一个关于查询的问题,该查询将返回多个表的结果。对不起,如果这真的很微不足道,我一直在尝试自己研究,但不知道要找的适当术语。

我有三张桌子:

Patient -
PatientID*
Surname
FName
DoB
Address
PhoneNo

Doctor -
DoctorID*
Surname
FName
DoB
Address
PhoneNo

Appointment -
AppointmentID*
DoctorID
PatientID
Time
Date

我希望查询返回所有约会,但将DoctorID和PatientID替换为匹配的医生姓氏和患者姓氏。所以返回AppointmentID,医生Surname,患者姓氏,时间和日期。

我希望我能够很好地解释这一点,以便有人理解!

提前感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

应该是内部联接

select a.AppointmentID, b.Surname, b.FNAME, c.Surname, c.FName
from Appointment as a
Inner join Doctor as b on b.DoctorID = a.DoctorID
Inner join Patient as c on c.PatientID = a,PatientID

答案 1 :(得分:0)

SELECT
   a.AppointmentID,
   a.Time,
   a.Date,
   d.Surname,
   p.Surname
FROM
   Appointment a with (nolock)
   left join
   Doctor d with (nolock) on
   d.DoctorID = a.DoctorID
   left join
   Patient p with (nolock) on
   p.PatientID = a.PatientID