在您的要求中存在一个根本性问题,那就是在您的表CandidateQualifications
中您想要DegreeID
,但您希望将其链接到3个表格。
一些示例性数据:
CandidateDetails
+--------------------------------------------------+
¦ 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从三个不同的表,一旦你插入数据你无法知道它链接到哪一个。
如果不是太晚了,我建议改变你的数据结构类似如下:
QualificationType
+----+---------------+
¦ 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;
让你qualifations然而,回答您的实际问题,您可以使用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);
任何人都有关于这个的想法? – Duk 2013-05-03 05:31:55
请提供一些明确的信息 – Anvesh 2013-05-03 06:12:02
您能举个例子吗?(测试数据) – 2013-05-03 07:34:00