如何比较具有相同值的多个列(Oracle SQL)

时间:2016-11-23 07:22:38

标签: sql oracle

我需要在单个镜头中比较具有相同值集的表的多个值,而不在AND子句中使用多个WHERE条件。请告诉我任何方法。

select count(1)
  from test
 where pre_sys_id1 in (select sys_id from test where auto_id = 10)
   and pre_sys_id2 in (select sys_id from test where auto_id = 10)
   and pre_sys_id3 in (select sys_id from test where auto_id = 10)
   and pre_sys_id4 in (select sys_id from test where auto_id = 10)
;

2 个答案:

答案 0 :(得分:1)

Select count(1)
from test
where  (select sys_id from test where auto_id=10) = all (pre_sys_id1,pre_sys_id2,pre_sys_id3,pre_sys_id4) 

或加入

Select count(1)
from test t1
join test t2 on( sys_id= pre_sys_id1 and sys_id= pre_sys_id2 and sys_id= pre_sys_id3  and sys_id= pre_sys_id4) 
where t2.auto_id = 10 ;

答案 1 :(得分:1)

您可以尝试:

select count(1)
  from test
 where (pre_sys_id1, pre_sys_id2, pre_sys_id3, pre_sys_id4)
   in (select sys_id, sys_id, sys_id, sys_id from test where auto_id = 10)