我有两张桌子。 表A具有以下内容:
UserID | ActiveFlag
000001 | 1
000002 | 1
000003 | 1
000004 | 1
表B:
ID | UserID | CustomerId
1 | 000001 | 10
2 | 000002 | 10
3 | 000002 | 11
4 | 000003 | 11
我想将ActiveFlag
中的Table A
列更新为0
,其中table B
中的用户CustomerID
10
且仅10
{1}}。这意味着,在示例中,UserID 000002
在Table B
中有两条记录:CustomerID
10
和11
。我不想更新00002
中的table A
。
答案 0 :(得分:1)
10,只有10
update table a set activeflag = o where a.userid in
(
select b.userid from table b group by b.userid
having min(b.customerid) = max(b.customerid)
and min(b.customerid) = 10
)
答案 1 :(得分:0)
exists
:
update tablea a
set activeflag = 0
where a.activeflag <> 0 and
exists (select 1 from b where b.userid = a.userid and b.customerid = 10) and
not exists (select 1 from b where b.userid = a.userid and b.customerid <> 10);
答案 2 :(得分:-2)
试试这个:
UPDATE tableA
SET ActiveFlag = 0
WHERE UserID IN (
SELECT UserID
FROM tableB
WHERE CustomerId = 10);
原则是:
它会过滤掉表B中UserID
= 10的所有CustomerId
,即000001(ID = 1)和000002(ID = 2)
检查步骤1中的每个UserID
,设置ActiveFlag = 0
。即表A中的前两行已更新