内部加入多个列

时间:2017-05-17 10:40:47

标签: sql

img 嗨,大家好。 我正在开发一个诊所管理程序,我必须加入两个表TB_DOCTORS和TB_SPECIALTY。问题是;每个医生都有4个专业。如何加入TB_DOCTORS表格而不仅仅是专业

1 个答案:

答案 0 :(得分:0)

SELECT TB_DOCTORS.*, 
S1.SPECIALITY_NAME AS SPECIALITY_NAME1, 
S2.SPECIALITY_NAME AS SPECIALITY_NAME2, 
S3.SPECIALITY_NAME AS SPECIALITY_NAME3, 
S4.SPECIALITY_NAME AS SPECIALITY_NAME4 
FROM TB_DOCTORS 
LEFT OUTER JOIN TB_SPECIALTY S1 ON TB_DOCTORS.SPEC_ID1 = S1.SPECIALITY_ID 
LEFT OUTER JOIN TB_SPECIALTY S2 ON TB_DOCTORS.SPEC_ID2 = S2.SPECIALITY_ID 
LEFT OUTER JOIN TB_SPECIALTY S3 ON TB_DOCTORS.SPEC_ID3 = S3.SPECIALITY_ID 
LEFT OUTER JOIN TB_SPECIALTY S4 ON TB_DOCTORS.SPEC_ID4 = S4.SPECIALITY_ID