[英]Results for IN and EXISTS
I have two table Table_hold
and table_pass
and I am using the following query to find out if any id in Table_pass exists, then delete the same from Table_hold.我有两个表Table_hold
和table_pass
,我正在使用以下查询来找出 Table_pass 中是否存在任何 id,然后从 Table_hold 中删除相同的。 I am using below query:我正在使用以下查询:
delete from Table_hold
where EXISTS ( SELECT 1 FROM Table_pass pass
WHERE id = pass.id );
Is there something wrong with this query?这个查询有问题吗? Even though there is Id=4 in table_hold and it's not there in table_pass, it still it got deleted from table_hold.即使 table_hold 中有 Id=4 并且它在 table_pass 中不存在,它仍然从 table_hold 中删除。
Below are the dummy tables I created:以下是我创建的虚拟表:
create table Table_pass(id1 number);
create table table_hold(id1 number);
insert into table_pass values(1);
insert into table_pass values(2);
insert into table_pass values(3);
insert into table_hold values(4);
delete from Table_hold
where EXISTS ( SELECT 1 FROM Table_pass pass
WHERE id1 = pass.id1 );
This query deletes one row with ID 4 in table_hold.此查询删除 table_hold 中 ID 为 4 的一行。
Qualify all column references!限定所有列引用!
delete from Table_hold
where exists (select 1
from Table_pass p
where p.id = table_hold.id
);
This subquery:这个子查询:
where EXISTS (SELECT 1
FROM Table_pass pass
WHERE id = pass.id
);
has a reference to id
.引用了id
。 How is it resolved?它是如何解决的? Well, from the inside out.嗯,由内而外。 So this is equivalent to:所以这相当于:
where EXISTS (SELECT 1
FROM Table_pass pass
WHERE pass.id = pass.id
--------------------^ NOT table_hold
);
The solution is to get in the habit of qualifying all column names.解决方案是养成限定所有列名的习惯。
try like below尝试如下
delete from Table_hold t1
where EXISTS ( SELECT 1 FROM Table_pass t2
WHERE t2.id1 = t1.id1 );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.