gpt4 book ai didi

sql - 根据第三个表中的列值删除表中的行

转载 作者:搜寻专家 更新时间:2023-10-30 20:18:35 25 4
gpt4 key购买 nike

我有三个表 Table1、Table2 和 Table3,下面的查询删除了 Table1 中的行

delete from Table1 
where EXISTS
(select (1) from Table2
where Table1.col1=Table2.col1
AND Table1.col2=Table2.col2
AND Table1.col3=(select **Table3.col3 from Table3** inner join Table2 on Table3.col1=Table2.col1)

这个查询是否正确?如果不是,如何在 where 条件中使用第三个表?

编辑:另外,如果我们想从本身与表 3 连接的表 2 中删除行,请解释如何重写查询?

最佳答案

这是一种方法:

delete from table1 
where (col1, col2, col3) in (
select t1.col1, t1.col2, t1.col3
from table1 t1
join table2 t2 on t1.col1 = t2.col1 and t1.col2 = t2.col2
join table3 t3 on t1.col3 = t3.col3 and t2.col1 = t3.col1
);

或者使用 EXISTS 可能会更快:

delete table1 
where exists (
select *
from table2
join table3 on table2.col1 = table3.col1
where table1.col3 = table3.col3 and
table1.col1 = table2.col1 and
table1.col2 = table2.col2);

关于sql - 根据第三个表中的列值删除表中的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19228491/

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