【问题标题】:Insert to first table based on another table基于另一个表插入到第一个表
【发布时间】:2023-03-04 23:57:01
【问题描述】:

我有以下人员表:

人:

| id   | f_name  | l_name    | role  |
|:-----|:-------:|:---------:| -----:|
| 1    | John    | Nathan    | 1     |
| 2    | Brand   |  Ba       | 1     |
| 3    | Bob     |  Do       | 2     |
| 4    | Alice   |  Sia      | 1     |

和用户表:

用户:

| id   | f_name  | l_name | role  |
|:-----|:-------:|:------:| -----:|
| 1    | John    | Tom    |   1   |

我希望 People 表中角色 = 1 的任何人都将出现在 Users 表中,因此如果他们不在 users 表中,我将它们插入到表中,如果它们确实存在,我会更新他们的第一个和最后一个基于 id 列的 People 表中的名称和角色

想要的结果:

用户:

| id   | f_name  | l_name    | role  |
|:-----|:-------:|:------:   | -----:|
| 1    | John    | Nathan    |   1   |
| 2    | Brand   | Ba        |   1   |
| 3    | Bob     | Do        |   1   |

提前致谢!

【问题讨论】:

  • 这称为MERGE 或“Upsert”。如果您搜索,您会发现大量有关如何执行此操作的信息。
  • 为什么要存储相同数据的两个副本?随便看看
  • 根据问题指南,请展示您的尝试并告诉我们您发现了什么(在本网站或其他地方)以及为什么它不能满足您的需求。
  • @DaleK 我最终只在特定条件源上使用MERGE。感谢大家的帮助。

标签: sql sql-server tsql merge


【解决方案1】:

根据@Larnu 的建议,您可以在 sql server 中使用 MERGE:

架构和插入语句

create table people( id int ,f_name  varchar(25), l_name varchar(25),role  int);
insert into people values( 1    ,'John', 'Nathan', 1);     
insert into people values( 2    ,'Brand','Ba'    , 1);     
insert into people values( 3    ,'Bob',  'Do'    , 2);     
insert into people values( 4    ,'Alice','Sia'   , 1);     


create table Users( id int,  f_name varchar(25), l_name varchar(25), role int);
insert into Users values( 1    ,'John' ,'Tom' ,1 );

合并查询:

MERGE Users AS TARGET
USING people AS SOURCE 
ON (TARGET.id = SOURCE.id) 
--When records are matched, update the records 
WHEN MATCHED 
THEN UPDATE SET TARGET.f_name = SOURCE.f_name, TARGET.l_name = SOURCE.l_name 
--When no records are matched, insert the incoming records from source table to target table
WHEN NOT MATCHED BY TARGET 
THEN INSERT (id, f_name, l_name,role) VALUES (SOURCE.id, SOURCE.f_name, SOURCE.l_name, SOURCE.role);

合并后:

  select * from users;

输出:

id f_name l_name role
1 John Nathan 1
2 Brand Ba 1
3 Bob Do 2
4 Alice Sia 1

db小提琴here

如果您只想合并具有角色 1 的人员:

架构和插入语句:

 create table people( id int ,f_name  varchar(25), l_name varchar(25),role  int);
 insert into people values( 1    ,'John', 'Nathan', 1);     
 insert into people values( 2    ,'Brand','Ba'    , 1);     
 insert into people values( 3    ,'Bob',  'Do'    , 2);     
 insert into people values( 4    ,'Alice','Sia'   , 1);     
 
 
 create table Users( id int,  f_name varchar(25), l_name varchar(25), role int);
 insert into Users values( 1    ,'John' ,'Tom' ,1 );

合并查询:

 MERGE Users AS TARGET
 USING (select * from people where role=1) AS SOURCE 
 ON (TARGET.id = SOURCE.id ) 
 --When records are matched, update the records 
 WHEN MATCHED 
 THEN UPDATE SET TARGET.f_name = SOURCE.f_name, TARGET.l_name = SOURCE.l_name , TARGET.role=SOURCE.role
 --When no records are matched, insert the incoming records from source table to target table
 WHEN NOT MATCHED BY TARGET 
 THEN INSERT (id, f_name, l_name,role) VALUES (SOURCE.id, SOURCE.f_name, SOURCE.l_name, SOURCE.role);

合并后:

 select * from users;

输出:

id f_name l_name role
1 John Nathan 1
2 Brand Ba 1
4 Alice Sia 1

db小提琴here

【讨论】:

  • 如果我只想合并所有role=1的列怎么办?
  • @liron29 我已经修改了答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-29
  • 2013-06-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多