我有以下查询:
select A,
B
from table1
where A in (select c
from table 2
)
但是,现在我需要更改此查询并使用exists
代替in
,并且应该给出相同的结果。
我的表格如下所示:
table1 table2
A B c
------ -----
1 x 1
2 y 3
3 z 4
4 w 7
5 a
1 b
我该如何使用exists
功能?
您需要匹配将在中使用的两列exists
一起:
select
t1.a, t1.b
from
table1 t1
where
exists (select 1 from table2 t2 where t2.c = t1.a)
你必须这样做的原因是因为exists
对表执行半连接,因此需要有连接条件。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)