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

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

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

问题

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

两个表的主键都是 Employee_number

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

  1. Select Distinct employee_number
  2. from dbo.tableB
  3. where employee_number not in (select distinct employee_number
  4. from dbo.tableA
  5. where pref Pref = 'work')
  6. and active_or_inactive = 'a';
  7. Update TableA
  8. Set value = 'good'
  9. Where employee_number in (select distinct employee_number
  10. from dbo.tableB
  11. where employee_number not in (select distinct employee_number
  12. from dbo.tableA
  13. where Pref = 'work')
  14. 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

  1. Select Distinct employee_number
  2. from dbo.tableB
  3. where employee_number not in (select distinct employee_number
  4. from dbo.tableA
  5. where pref Pref = 'work')
  6. and active_or_inactive = 'a'
  7. Update TableA
  8. Set value = good
  9. Where employee_number from dbo.tableB
  10. where employee_number not in (employee_number
  11. from do.tableA
  12. where Pref = 'work')
  13. and active_or_inactive = 'a'

答案1

得分: 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:

  1. UPDATE a
  2. SET value = 'good'
  3. FROM TableA a
  4. WHERE NOT EXISTS (
  5. SELECT 1
  6. FROM TableB b
  7. WHERE b.employee_number = a.employee_number
  8. AND b.active_or_inactive = 'a'
  9. )
  10. 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:

确定