组合一个到多个表并获取值

时间:2013-05-03 05:05:18

标签: sql sql-server sql-server-2008 select

我创建了插入查询,用于组合一个到多个表,并将值合并为单个。

表1:

CandidateQualifications:

  • 编号
  • CandidateId
  • DegreeId
  • 专业化

表2:

UG_list

  • 编号
  • Ugname

表3:

PG_list

  • 编号
  • pgname

表4:

Docorate_List

  • 编号
  • Ugname

调用这些所有表id以跟随表,

CandidateDetails

  • 认证
  • Postgraduation
  • 博士学位

它显示了此candidatedetails表中的单独度。当我使用跟随查询时,我只获得资格值。其他人被淘汰。因此,如果3度,则表示candidateid显示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 个答案:

答案 0 :(得分:0)

您的要求中存在一个根本问题,即您希望CandidateQualificationsDegreeID中有+--------------------------------------------------+ ¦ ID ¦ Qualification ¦ Postgraduation ¦ doctorate ¦ ¦----+----------------+----------------+-----------¦ ¦ 1 ¦ Qualification1 ¦ PostGraduation1¦ doctorate1¦ ¦ 2 ¦ Qualification2 ¦ PostGraduation2¦ doctorate2¦ ¦ 3 ¦ Qualification3 ¦ PostGraduation3¦ doctorate3¦ +----+----------------+----------------+-----------+ ,但您希望将其链接到3个表格。

使用一些示例数据:

<强> CandidateDetails

+---------------------+
¦ ID ¦      Ugname    ¦
¦----+----------------¦
¦  1 ¦ Qualification1 ¦ 
¦  2 ¦ Qualification2 ¦ 
¦  3 ¦ Qualification3 ¦ 
+----+----------------+

<强> UG_list

+---------------------+
¦ ID ¦      pgname    ¦
¦----+----------------¦
¦  1 ¦ PostGraduation1¦ 
¦  2 ¦ PostGraduation2¦
¦  3 ¦ PostGraduation3¦ 
+----+----------------+

<强> PG_list

+-----------------+
¦ ID ¦  dcname    ¦
¦----+------------¦
¦  1 ¦ doctorate1 ¦ 
¦  2 ¦ doctorate2 ¦
¦  3 ¦ doctorate3 ¦ 
+----+------------+

<强> Docorate_List

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 ¦      Name     ¦
¦----+---------------¦
¦  1 ¦ UnderGraduate ¦ 
¦  2 ¦  PostGraduate ¦
¦  3 ¦   Doctorate   ¦ 
+----+---------------+

问题在于,因为你有来自三个不同表的ID,所以一旦插入了数据,就无法知道它链接回哪一个。

如果还不算太晚,我建议您将数据结构更改为如下所示:

<强> QualificationType

+----+---------------------+-----------------+
¦ 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   ¦
+----+---------------------+-----------------+

<强>度

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);

然后您的CandidateQualifation表可以引用QualifationID,然后您确切地知道它引用的内容。

所以你的插入查询变成了(我不确定专业化的来源):

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;

您可以使用类似

的内容获得资格
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);

但是,要回答您的实际问题,您可以使用OUTER APPLY获得单独的限定为sepearate行:

{{1}}