组合一个到多个表并获取值
Posted
技术标签:
【中文标题】组合一个到多个表并获取值【英文标题】:combine one to more tables and get the values 【发布时间】:2013-05-03 05:05:18 【问题描述】:我创建了插入查询,用于组合一个到多个表并将值合并到单个表中。
表 1:
候选人资格:
身份证 候选人身份 学位ID 专业化表 2:
UG_list
身份证 小名表 3:
PG_list
身份证 pgname表 4:
Docorate_List
身份证 小名这些所有的表id都被调用来跟随表,
候选人详情
资质 研究生 博士学位它在此候选详细信息表中显示单独的度数。当我使用跟随查询时,我只得到限定值。其他人被淘汰。因此,如果有 3 度,则表示候选 ID 显示 3 次。但只显示一次。
我的查询是,
SELECT top(50) 'INSERT INTO CandidateQualifications(candidateId,DegreeId,specialization) VALUES('+
Cast(c.CandidateID as varchar(50))+',''' +
ISNULL(Cast(Coalesce(u.Id,p.Id,d.Id)as varchar(50)),'NULL')+','+
IsNull(''''+c.ugspecification+'''', 'NULL')+')'
FROM candidatedetails as c
LEFT join UG_List As u ON c.qualification=u.UGName
LEFT join PG_List As p ON c.PostGraduation=p.PGName
LEFT join Docorate_List As d ON c.Doctorate=d.Doctorate
结果集
INSERT INTO CandidateQualifications(candidateId,DegreeId,specialization) VALUES(2,'38,'Hotel Management')
INSERT INTO CandidateQualifications(candidateId,DegreeId,specialization) VALUES(3,'17,'HMCT (Hotel Management& Catering technology)')
请任何人帮我解决这个问题?
【问题讨论】:
有人知道这个吗? 请提供明确的信息 你能举个例子吗?(测试数据) 【参考方案1】:您的要求存在一个基本问题,即在您的表 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,一旦插入数据,您就无法知道它链接回哪一个。
如果还不算太晚,我建议您将数据结构更改为如下所示:
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;
但是,要回答您的实际问题,您可以使用 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);
【讨论】:
以上是关于组合一个到多个表并获取值的主要内容,如果未能解决你的问题,请参考以下文章