您的要求存在一个基本问题,即在您CandidateQualifications
想要的表中DegreeID
,但您希望它链接到 3 个表。
使用一些示例数据:
候选人详情
+--------------------------------------------------+
¦ ID ¦ Qualification ¦ Postgraduation ¦ doctorate ¦
¦----+----------------+----------------+-----------¦
¦ 1 ¦ Qualification1 ¦ PostGraduation1¦ doctorate1¦
¦ 2 ¦ Qualification2 ¦ PostGraduation2¦ doctorate2¦
¦ 3 ¦ Qualification3 ¦ PostGraduation3¦ doctorate3¦
+----+----------------+----------------+-----------+
UG_list
+---------------------+
¦ ID ¦ Ugname ¦
¦----+----------------¦
¦ 1 ¦ Qualification1 ¦
¦ 2 ¦ Qualification2 ¦
¦ 3 ¦ Qualification3 ¦
+----+----------------+
PG_list
+---------------------+
¦ ID ¦ pgname ¦
¦----+----------------¦
¦ 1 ¦ PostGraduation1¦
¦ 2 ¦ PostGraduation2¦
¦ 3 ¦ PostGraduation3¦
+----+----------------+
Docorate_List
+-----------------+
¦ ID ¦ dcname ¦
¦----+------------¦
¦ 1 ¦ doctorate1 ¦
¦ 2 ¦ doctorate2 ¦
¦ 3 ¦ doctorate3 ¦
+----+------------+
您最终希望得到的结果CandidateQualifications
是
+-----------------------------------------------+
¦ ID ¦ CandidateId ¦ DegreeId ¦ Specialization ¦
¦----+--------------+----------+----------------¦
¦ 1 ¦ 1 ¦ 1 ¦ Spec1 ¦ -- Qualification1
¦ 2 ¦ 1 ¦ 1 ¦ Spec1 ¦ -- PostGraduation1
¦ 3 ¦ 1 ¦ 1 ¦ Spec1 ¦ -- doctorate1
¦ 4 ¦ 2 ¦ 2 ¦ Spec2 ¦ -- Qualification2
¦ 5 ¦ 2 ¦ 2 ¦ Spec2 ¦ -- PostGraduation2
¦ 6 ¦ 2 ¦ 2 ¦ Spec2 ¦ -- doctorate2
¦ 7 ¦ 3 ¦ 3 ¦ Spec3 ¦ -- Qualification3
¦ 8 ¦ 3 ¦ 3 ¦ Spec3 ¦ -- PostGraduation3
¦ 9 ¦ 3 ¦ 3 ¦ Spec3 ¦ -- doctorate3
+----+--------------+----------+----------------+
问题是因为你有来自三个不同表的 ID,一旦你插入了数据,你就无法知道它链接回哪一个。
如果还不算太晚,我建议将您的数据结构更改为如下所示:
资格类型
+----+---------------+
¦ ID ¦ Name ¦
¦----+---------------¦
¦ 1 ¦ UnderGraduate ¦
¦ 2 ¦ PostGraduate ¦
¦ 3 ¦ Doctorate ¦
+----+---------------+
程度
+----+---------------------+-----------------+
¦ ID ¦ QualificationTypeID ¦ Name ¦
¦----+---------------------¦-----------------¦
¦ 1 ¦ 1 ¦ Qualifacation1 ¦
¦ 2 ¦ 1 ¦ Qualifacation2 ¦
¦ 3 ¦ 1 ¦ Qualifacation3 ¦
¦ 4 ¦ 2 ¦ PostGraduation1 ¦
¦ 5 ¦ 2 ¦ PostGraduation2 ¦
¦ 6 ¦ 2 ¦ PostGraduation3 ¦
¦ 7 ¦ 3 ¦ Doctorate1 ¦
¦ 8 ¦ 3 ¦ Doctorate2 ¦
¦ 9 ¦ 3 ¦ Doctorate3 ¦
+----+---------------------+-----------------+
然后你的 CandidateQualifation 表可以引用 QualifationID,然后你就知道它引用了什么。
因此,您的插入查询变为(我不确定专业化来自何处):
INSERT CandidateQualifation (CandidateID, DegreeID)
SELECT cd.CandidateID,
dg.DegreeID
FROM CandidateDetails cd
LEFT JOIN Degree ug
ON cd.qualification = ug.Name
AND cd.QualificationTypeID = 1
LEFT JOIN Degree pg
ON cd.PostGraduation = pg.Name
AND cd.QualificationTypeID = 2
LEFT JOIN Degree doc
ON cd.Doctorate = doc.Name
AND cd.QualificationTypeID = 3
OUTER APPLY
( VALUES
(ug.ID),
(pg.ID),
(doc.ID)
) dg (DegreeID);
你可以使用类似的东西来获得你的资格
SELECT cd.CandidateID,
Qualification = Degree.Name,
QualificationType = qt.Name
FROM CandidateDetails cd
INNER JOIN CandidateQualifications cq
ON cd.ID = cq.CandidateID
INNER JOIN Degree
ON Degree.ID = cq.DegreeID
INNER JOIN QualificationType qt
ON qt.ID = Degree.QualificationTypeID;
但是,要回答您的实际问题,您可以使用 OUTER APPLY 将单独的限定作为单独的行:
SELECT cd.CandidateID,
d.DegreeID
FROM CandidateDetails cd
LEFT JOIN UG_List
ON cD.qualification = UG_List.UGName
LEFT JOIN PG_List As p
ON cd.PostGraduation = PG_List.PGName
LEFT JOIN Docorate_List As d
ON cD.Doctorate = Docorate_List.Doctorate
OUTER APPLY
( VALUES
(UG_List.ID),
(PG_List.ID),
(UG_List.ID)
) d (DegreeID);