gpt4 book ai didi

sql - 选择一个变量

转载 作者:行者123 更新时间:2023-11-29 14:07:08 26 4
gpt4 key购买 nike

我有很多命令,例如:

DELETE FROM table1 WHERE user_id IN (SELECT id FROM users WHERE email LIKE 'abc%');
DELETE FROM table2 WHERE user_id IN (SELECT id FROM users WHERE email LIKE 'abc%');
DELETE FROM table3 WHERE user_id IN (SELECT id FROM users WHERE email LIKE 'abc%');

如何通过提取变量/列表/数组/集合来优化它?

SELECT id FROM users WHERE email LIKE 'abc%'

最佳答案

如果您的目标是只写一次删除哪个用户的条件,您可以使用数据修改 CTE:

with user_list as (
select id
from users
where email like 'abc%'
), delete_1 as (
delete from table_1 where user_id in (select id from user_list)
), delete_1 as (
delete from table_2 where user_id in (select id from user_list)
)
delete from table_3 where user_id in (select id from user_list);

您仍然需要为每次删除重复select ...,但是关于需要删除的内容的条件在您的语句中只有一次。

关于sql - 选择一个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24700107/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com