sql语句 如何判断A表中的a列数据是否在B表中的b列中存在

来源:百度知道 编辑:UC知道 时间:2024/05/31 16:12:58
如题 A表中有a列 假设数据为1,2,3
B表中有b列 假设数据为2,3,4
现在需要判断a列中的数据在b列中存在

select A.a from A,B where A.a=B.b 最简单的判断。
用IN的话可能出错:select a from A where a IN(select b from B)
用exists如楼上所说~

--存在
select *
from A
where exists(select 1 from B where A.a = B.b)

--不存在
select *
from A
where not exists(select 1 from B where A.a = B.b)

用in语法

SELECT a from A
WHERE a in (select b from B)