首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SQL更新两条记录

SQL更新两条记录
EN

Stack Overflow用户
提问于 2016-04-11 14:56:40
回答 2查看 49关注 0票数 0

我正在尝试更新两个员工的数据。假设一个雇员的id是1234,另一个雇员的id是5678。每个员工都有不同的服务。

这是我的参考表格

代码语言:javascript
复制
serv_id          emp_id      serv_name    status 
1                 1234        computer1     A
5                 1234        computer2     A
10                1234        computer3     A
37                1234        computer4     A
5                 5678        computer2     A
11                5678        projector1    A
12                5678        projector2    A
30                5678        projector3    A
37                5678        computer4     A   
35                5678        projector4    A

在上表中,我们有2个员工,有10条记录,每个员工有5个服务。我现在要做的是将这两个员工的服务合并为一个员工(1234),因为员工5678被解雇了。但是,employee 1234中不需要更新带有“5”或“37”的serv_id。

这是我的预期结果。假设A是活动的,而I是不活动的。对如何做有什么建议吗?

代码语言:javascript
复制
serv_id (pk)                 emp_id (fk)     serv_name    status 
1                            1234            computer1      A
5 (no update)                1234            computer2     A
10                           1234            computer3     A
11                           1234            projector1    A
12                           1234            projector2    A
30                           1234            projector3    A
35                           1234            projector4    A
37 (no update)               1234            computer4     A
11                           5678            projector1    I
37                           5678            computer4     I
5                            5678            computer2     I
11                           5678            projector1    I
12                           5678            projector2    I
30                           5678            projector3    I
37                           5678            computer4     I
35                           5678            projector4    I
EN

回答 2

Stack Overflow用户

发布于 2016-04-11 15:13:39

尝尝这个

代码语言:javascript
复制
declare @tb as Table (serv_id int, emp_id int, serv_name nvarchar(20),status nvarchar(5))
insert into @tb 
select 1 ,1234,'computer1','A' union
select 5 ,1234,'computer2','A' union
select 10,1234,'computer3','A' union
select 37,1234,'computer4','A' union
select 5 ,5678,'computer2','A' union
select 11,5678,'projector1','A' union
select 12,5678,'projector2','A' union
select 30,5678,'projector3','A' union
select 37,5678,'computer4','A'  union
select 35,5678,'projector4','A'

update @tb set status ='I' where emp_id = 5678

insert into @tb select serv_id,1234,serv_name,'A' from @tb where emp_id=5678 and serv_id not in (select serv_id from @tb where emp_id=1234)

select * from @tb order by emp_id,serv_id
票数 0
EN

Stack Overflow用户

发布于 2016-04-11 15:13:40

代码语言:javascript
复制
UPDATE t1
SET t1.status='I'
FROM my_table t1
       LEFT OUTER JOIN my_table t2 ON t2.serv_name=t1.serv_name AND t2.emp_id=1234   
WHERE t1.emp_id=5678
  AND t2.serv_id IS NULL

我们可以通过serv_nameemp_id将表连接到表本身,以找到通用的serv_name,然后只更新没有相同emp_id=1234值的记录

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36541797

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档