1

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

表格1:

候选人资格:

  • ID
  • 候选人 ID
  • 学位编号
  • 专业化

表 2:

UG_list

  • ID
  • 名字

表3:

PG_list

  • ID
  • 名称

表 4:

Docorate_List

  • ID
  • 名字

这些所有表的 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)')

请帮我解决这个问题?

4

1 回答 1

0

您的要求存在一个基本问题,即在您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);
于 2013-05-03T08:52:17.353 回答