如何在表B中存在条件时更新表A

huangapple go评论65阅读模式
英文:

How to update Table A, when there are conditions in Table B

问题

我试图运行一个查询,仅在满足以下条件的情况下更新 SQL Server 表(表 A)中第 3 列的值,条件为该条件存在于表 B 中并且存在于表 A 的第 2 列(pref)中。

两个表的主键都是 Employee_number

并且表 B 中的条件是活动或非活动的。

Select Distinct employee_number 
from dbo.tableB  
where employee_number not in (select distinct employee_number 
                              from dbo.tableA 
                              where pref Pref = 'work') 
  and active_or_inactive = 'a';

Update TableA
Set value = 'good'
Where employee_number in (select distinct employee_number 
                          from dbo.tableB  
                          where employee_number not in (select distinct employee_number 
                                                      from dbo.tableA 
                                                      where Pref = 'work') 
                            and active_or_inactive = 'a');
英文:

I am trying to run a query that updates a SQL Server table (Table A) value in column 3 only on the include condition that is present in Table B and in column 2 (pref) of Table A

The primary key on both tables is Employee_number.

And the condition in Table B active or inactive

Select Distinct employee_number 
from dbo.tableB  
where employee_number not in (select distinct employee_number 
                              from dbo.tableA 
                              where pref Pref = 'work') 
  and active_or_inactive = 'a'


Update TableA
Set value = “good”
Where employee_number from dbo.tableB  
where employee_number not in (employee_number 
                              from do.tableA 
                              where Pref = 'work') 
  and active_or_inactive = 'a'

答案1

得分: 1

这个示例代码中,如何所有部分拼合在一起并不是完全清楚,但可以尝试这样做:

UPDATE a
SET value = '好'
FROM TableA a
WHERE NOT EXISTS (
SELECT 1
FROM TableB b
WHERE b.employee_number = a.employee_number
AND b.active_or_inactive = 'a'
)
AND a.Pref='工作'

英文:

It's not entirely clear how everything fits together in the sample code, but try this:

UPDATE a
SET value = 'good'
FROM TableA a
WHERE NOT EXISTS (
        SELECT 1 
        FROM TableB b
        WHERE b.employee_number = a.employee_number
            AND b.active_or_inactive = 'a'
   )
   AND a.Pref='work'

huangapple
  • 本文由 发表于 2023年5月17日 07:31:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/76267703.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定