日期:2014-05-17  浏览次数:20892 次

一个select要求其中的某个字段必须在另一个select中存在怎么写sql?
比如我
集合1.select A_no from taba,tabc,tabd where *****************

集合2.select A_no from taba,tabb where *****************

集合1里可能查出重复的A_no,比如
A_no
a
a
b
b
c
d

集合2里主要是查出有效的A_no,里面A_no不重复,比如
a
c

我现在想求出的结果
A_no
a
a
c


怎么办呢。谢谢




------解决方案--------------------
集合1
union
集合2
------解决方案--------------------
select A_no from taba,tabc,tabd where *****************
where A_no in
(select A_no from taba,tabb where *****************)

------解决方案--------------------
探讨

select A_no from taba,tabc,tabd where *****************
where A_no in
(select A_no from taba,tabb where *****************)

------解决方案--------------------
SQL code

集合1.select A_no from taba,tabc,tabd where *****************
UNION ALL
集合2.select A_no from taba,tabb where *****************

------解决方案--------------------

给多一个不相同的字段B2,
SQL code
集合1.select A_no,B2 from taba,tabc,tabd where *****************
INTERSECT
集合2.select A_no,B2 from taba,tabb where *****************

------解决方案--------------------
select A_no from taba,tabc,tabd where A_no in
(select A_no from taba,tabb where *****************)
and 其它条件
------解决方案--------------------
select A_no from taba,tabc,tabd where *****************

and A_no in ( select A_no from taba,tabb where ***************** )


------解决方案--------------------
select t1.a_no
from (select A_no from taba,tabc,tabd where *****************) t1
where exists
(select 1 from (select A_no from taba,tabb where *****************) t2
where t2.a_no = t1.a_no)