【问题标题】:How to populate a table based on a value from a different table如何根据来自不同表的值填充表
【发布时间】:2021-11-05 17:40:28
【问题描述】:

我有两个数据表,我可以使用在两个表中的 ID 上链接的左连接来连接它们。如果课程和人员相同,我需要将 RegNumber 填充为与已经存在 1 行的 RegNumber 相同:

目前的情况:如果我用左连接连接表 1 和表 2。

表 1

ID      |    Course|    Person
67705   |    A     |    1
68521   |    A     |    1
85742   |    A     |    1 
89625   |    A     |    1
67857   |    B     |    2
86694   |    B     |    2
88075   |    B     |    2
88710   |    C     |    3
47924   |    C     |    3
66981   |    C     |    3
12311   |    B     |    1
12312   |    B     |    1
12313   |    B     |    1

表 2

ID      |   RegNumber
67705   |    N712316
NULL    |    NULL
NULL    |    NULL
NULL    |    NULL
67857   |    N712338
NULL    |    NULL
NULL    |    NULL
NULL    |    NULL
47924   |    M481035
NULL    |    NULL
12311   |    N645525
NULL    |    NULL
NULL    |    NULL

我需要表 2 看起来像这样:

ID      |  RegNumber
67705   |  N712316
68521   |  N712316
85742   |  N712316
89625   |  N712316
67857   |  N712338
86694   |  N712338
88075   |  N712338
88710   |  N712338
47924   |  M481035
66981   |  M481035
12311   |  N645525
12312   |  N645525
12313   |  N645525

也就是说,我需要在表2中插入新行

谁能帮帮我?这完全超出了我的能力范围!

【问题讨论】:

  • 所以,我有点困惑。您是说您的表 1 示例有 13 条记录,而您的表 2 示例有 4 条记录,然后您离开加入它们并得到第二个插图中的结果?

标签: sql sql-server sql-insert


【解决方案1】:
insert into table2 (ID,RegNumber)
select t1.ID,reg.regNumber
from table1 t1
cross join (select top 1 regNumber from table2 r2 join table1 r1 
            on r1.Id = r2.Id 
            and r1.Course = t1.Course 
            and r1.Person = t1.person 
            order by id) reg
where not exists (select 1 from table2 t2 where t1.ID = t2.ID)

您可以通过先将数据加载到临时表中来稍微提高性能:

select t1.ID , Course,Person,regNumber
into #LoadedData
from table1 t1
join table2 t2 on t1.Id = t2.ID 


insert into table2 (ID,RegNumber)
select t1.ID,reg.regNumber
from table1 t1
cross join (select top 1 regNumber from #LoadedData l
            where l.Course = t1.Course 
            and l.Person = t1.person 
            order by id) reg
where not exists (select 1 from  #LoadedData l where t1.ID = l.ID)

在任何一种情况下,对(ID、Course、Person)都有一个索引将有助于提高性能

【讨论】:

  • 感谢您的及时答复。这是行不通的,因为实际上我的桌子很大,而且有很多不同的人和课程。我会修改我的帖子。
  • 好的 - 我用更多数据编辑了我的原始帖子。对于每个相同的人员和课程,我需要已经存在 1 个 IDS 的 RegNumber,并且我需要所有具有相同人员和课程的 IDS 的注册号
  • @Shuffty123 查看更新的答案
【解决方案2】:

假设:

  1. 表 2 中缺少从表 1 中其他记录继承数据的项目。
  2. 让两个不同的 ID 共享同一个 Regnumber 的原因是课程号和人员号相同。

您确实需要将表 1 与其自身连接以创建将 ID 67705 与 ID 68521 关联的映射,然后您可以加入表 2 以获取 Regnumber。

试试这个:

Insert into table2 (ID,RegNumber)
Select right1.ID, left2.RegNumber
From (
    (table2 left2 INNER JOIN
     table1 left1 On (left1.ID=left2.ID)
     INNER JOIN table1 right1 On (left1.Course=right1.Course AND left1.Person=right1.Person)
    ) LEFT OUTER JOIN table2 right2 On (right1.ID=right2.ID)
WHERE right2.ID Is Null

第 4 个表连接(别名 right2)纯粹是防御性的,用于处理 table2 中的两条记录,在 table1 中具有相同的 Person & Course。

【讨论】:

  • 非常感谢克里斯
【解决方案3】:

我自己解决了这个问题。 我将人员和课程列连接起来,然后使用新的连接字段将它们连接起来

insert into table 2 (ID,RegNumber)
select X1.ID,X2.Regnumber 
from (select concat(course,person) as X,ID from table1) X1
join (select concat(t1.course,t1.person) as X, t2.RegNumber 
      from table1 t1
      join table2 t2 on t1.ID = t2.ID) X2 
on X1.X = X2.X
where X1.ID not in (select ID from table2)

【讨论】:

    猜你喜欢
    • 2020-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多