英文:
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'
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论