SQL服务器:如何删除基于另一个table
SQL Server : how to delete based on another table
我有一个 USERS
table,其中包含活跃用户和非活跃用户,我还有另一个名为 Leaders
的 table,其中存储了团队负责人(因此用户列表).
我想删除 table Leaders
中那些在 table users
.
中不活跃的用户
根据评论进行编辑:
Users
table: ID 和 Active
Leaders
table: ID
您可以使用 in
条件:
DELETE
FROM leaders
WHERE id IN (SELECT id
FROM users
WHERE active = 0 -- Or however you mark inactive users
)
您可以在删除中加入,类似于select:
delete ld
from leaders ld
join users us on ld.idUser = us.idUser
where us.active = 0
我有一个 USERS
table,其中包含活跃用户和非活跃用户,我还有另一个名为 Leaders
的 table,其中存储了团队负责人(因此用户列表).
我想删除 table Leaders
中那些在 table users
.
根据评论进行编辑:
Users
table: ID 和 ActiveLeaders
table: ID
您可以使用 in
条件:
DELETE
FROM leaders
WHERE id IN (SELECT id
FROM users
WHERE active = 0 -- Or however you mark inactive users
)
您可以在删除中加入,类似于select:
delete ld
from leaders ld
join users us on ld.idUser = us.idUser
where us.active = 0