我有两张桌子:
users
带user_id, status
柱子changes
有user_id, type
列
我想从changes
和 if中删除一行type = 5
,然后设置:
users.status = NULL Where users.user_id = changes.user_id
我该怎么做?
我有两张桌子:
users
带user_id, status
柱子changes
有user_id, type
列我想从changes
和 if中删除一行type = 5
,然后设置:
users.status = NULL Where users.user_id = changes.user_id
我该怎么做?
尝试这个:
-- table variable to store deleted changes
declare @deleted_changes table(user_id int, [type] int);
-- saving info from deleted changes into temp table
delete from changes
output deleted.user_id, deleted.[type] into @deleted_changes;
-- where [some condition]
-- updating users only if corresponding changes with type = 5 were deleted
update users
set status = null
where user_id in (select user_id from @deleted_changes where [type] = 5);
对于DELETE
,你的意思是...
DELETE FROM Changes WHERE type = 5
尝试以下更新....
UPDATE u
SET u.status = NULL
FROM users u
INNER JOIN changes c ON u.user_id = c.user_id