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

Sql 合并一个到多个表并获取值,sql,sql-server,sql-server-2008,select,Sql,Sql Server,Sql Server 2008,Select,我创建了insert查询,用于组合一到多个表,并将值放入单个表中 表1: 候选人资格: 身份证 候选人 脱脂 专业化 表2: UG_列表 身份证 乌格名称 表3: PGU列表 身份证 pgname 表4: 医生名单 身份证 乌格名称 调用这些所有表id以跟踪表 候选细节 +--------------------------------------------------+ ¦ ID ¦ Qualification ¦ Postgraduation ¦ doctorate ¦

我创建了insert查询,用于组合一到多个表,并将值放入单个表中

表1:

候选人资格:

  • 身份证
  • 候选人
  • 脱脂
  • 专业化
表2:

UG_列表

  • 身份证
  • 乌格名称
表3:

PGU列表

  • 身份证
  • pgname
表4:

医生名单

  • 身份证
  • 乌格名称
调用这些所有表id以跟踪表

候选细节

+--------------------------------------------------+
¦ ID ¦ Qualification  ¦ Postgraduation ¦ doctorate ¦
¦----+----------------+----------------+-----------¦
¦  1 ¦ Qualification1 ¦ PostGraduation1¦ doctorate1¦
¦  2 ¦ Qualification2 ¦ PostGraduation2¦ doctorate2¦
¦  3 ¦ Qualification3 ¦ PostGraduation3¦ doctorate3¦
+----+----------------+----------------+-----------+
  • 资格
  • 毕业后
  • 博士学位
它在这个候选明细表中显示了单独的学位。当我使用follow查询时,我只得到限定值。其他人则被淘汰。所以如果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)')
+---------------------+
¦ ID ¦      Ugname    ¦
¦----+----------------¦
¦  1 ¦ Qualification1 ¦ 
¦  2 ¦ Qualification2 ¦ 
¦  3 ¦ Qualification3 ¦ 
+----+----------------+
+---------------------+
¦ ID ¦      pgname    ¦
¦----+----------------¦
¦  1 ¦ PostGraduation1¦ 
¦  2 ¦ PostGraduation2¦
¦  3 ¦ PostGraduation3¦ 
+----+----------------+
+-----------------+
¦ ID ¦  dcname    ¦
¦----+------------¦
¦  1 ¦ doctorate1 ¦ 
¦  2 ¦ doctorate2 ¦
¦  3 ¦ doctorate3 ¦ 
+----+------------+

请帮助我找出这个问题?

您的需求中有一个基本问题,在您的表
候选资格中,您希望有
DegreeID
,但您希望它链接到3个表

使用一些示例数据:

候选细节

+--------------------------------------------------+
¦ ID ¦ Qualification  ¦ Postgraduation ¦ doctorate ¦
¦----+----------------+----------------+-----------¦
¦  1 ¦ Qualification1 ¦ PostGraduation1¦ doctorate1¦
¦  2 ¦ Qualification2 ¦ PostGraduation2¦ doctorate2¦
¦  3 ¦ Qualification3 ¦ PostGraduation3¦ doctorate3¦
+----+----------------+----------------+-----------+
UG_列表

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)')
+---------------------+
¦ ID ¦      Ugname    ¦
¦----+----------------¦
¦  1 ¦ Qualification1 ¦ 
¦  2 ¦ Qualification2 ¦ 
¦  3 ¦ Qualification3 ¦ 
+----+----------------+
+---------------------+
¦ ID ¦      pgname    ¦
¦----+----------------¦
¦  1 ¦ PostGraduation1¦ 
¦  2 ¦ PostGraduation2¦
¦  3 ¦ PostGraduation3¦ 
+----+----------------+
+-----------------+
¦ ID ¦  dcname    ¦
¦----+------------¦
¦  1 ¦ doctorate1 ¦ 
¦  2 ¦ doctorate2 ¦
¦  3 ¦ doctorate3 ¦ 
+----+------------+
pgu列表

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)')
+---------------------+
¦ ID ¦      Ugname    ¦
¦----+----------------¦
¦  1 ¦ Qualification1 ¦ 
¦  2 ¦ Qualification2 ¦ 
¦  3 ¦ Qualification3 ¦ 
+----+----------------+
+---------------------+
¦ ID ¦      pgname    ¦
¦----+----------------¦
¦  1 ¦ PostGraduation1¦ 
¦  2 ¦ PostGraduation2¦
¦  3 ¦ PostGraduation3¦ 
+----+----------------+
+-----------------+
¦ ID ¦  dcname    ¦
¦----+------------¦
¦  1 ¦ doctorate1 ¦ 
¦  2 ¦ doctorate2 ¦
¦  3 ¦ doctorate3 ¦ 
+----+------------+
文档列表

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)')
+---------------------+
¦ ID ¦      Ugname    ¦
¦----+----------------¦
¦  1 ¦ Qualification1 ¦ 
¦  2 ¦ Qualification2 ¦ 
¦  3 ¦ Qualification3 ¦ 
+----+----------------+
+---------------------+
¦ ID ¦      pgname    ¦
¦----+----------------¦
¦  1 ¦ PostGraduation1¦ 
¦  2 ¦ PostGraduation2¦
¦  3 ¦ PostGraduation3¦ 
+----+----------------+
+-----------------+
¦ 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   ¦
+----+---------------------+-----------------+
学位

+----+---------------+
¦ 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,这样您就可以确切地知道它引用了什么

因此,您的插入查询变为(我不确定专业化从何而来):

你可以通过以下方式获得你的资格

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

有人对此有想法吗?请提供一些明确的信息。你能举个例子吗?(测试数据)