如何从MySQL查询中的第三个表中获取信息?

时间:2016-03-09 13:35:29

标签: mysql

TABLE appointment (AppointmentNumber, ApptDate, PatientNumber, DentistID)

TABLE patientstable PatientNumber, PatientForename, PatientSurname, DentistID

TABLE dentists DentistID, DentistForename, DentistSurname

我正在运行

SELECT appointment.AppointmentNumber, appointment.ApptDate, 
patientstable.PatientForename, patientstable.PatientSurname 
FROM patientstable 
INNER JOIN appointment ON (patientstable.PatientNumber=appointment.PatientNumber) 

其中提供了每次约会的日期和患者姓名详情。

我想显示与每个约会相关的DentistForenam e和DentistSurname,但我不知道该怎么做。

2 个答案:

答案 0 :(得分:2)

你需要的只是另一个加入牙医表这样:

SELECT appointment.AppointmentNumber,
       appointment.ApptDate,
       patientstable.PatientForename,
       patientstable.PatientSurname,
       dentist.DentistForename,
       dentist.DentistSurname
FROM patientstable
INNER JOIN appointment 
 ON patientstable.PatientNumber=appointment.PatientNumber
INNER JOIN dentists
 ON(appointment.DentistID = dentist.DentistID)

答案 1 :(得分:0)

SELECT appointment.AppointmentNumber,appointment.ApptDate,patientstable.PatientForename,patientstable.PatientSurname 来自耐心稳定 INNER JOIN预约  ON patientstable.PatientNumber = appointment.PatientNumber 内联合牙医 在patientstable.dentistsid = dentists.dentistsid

加入另一个Tbale以获取牙医表中的列