select查询出错single-row subquery returns more than one row


出错原因是
select x from tablea where column=(select b from tableb)
而子查询返回的结果不止一条

按这个情况查询,有没有办法解决不出错又能查询到?


select x from tablea where column=any(select b from tableb)


select x from tablea where column in(select b from tableb);


一般 “1 = N”时就会报这个错,看你的实际用途,是在一个范围内的话可以采用上述方法,这种错误也经常在DML 语句中出现!


引用 1 楼 suiziguo 的回复:
select x from tablea where column=any(select b from tableb)

可以实现



引用 2 楼 adebayor 的回复:
select x from tablea where column in(select b from tableb);

支持.


select x from tablea where column=(select b from tableb where rownum=1)



SQL code

SELECT a.x

FROM tablea a, tableb b

WHERE a.COLUMN = b.b




几种方法,效率各不同

如2楼
select x from tablea where column in(select b from tableb);

还可以用EXISTS,大部分情况比IN的效率高
select x from tablea a where exists 
(select 1 from tableb b
where b.b=a.column
);



1,select x from tablea where column=any(select b from tableb)

2,select x from tablea where column in(select b from tableb);

3,select x from tablea a where exists 
(select 1 from tableb b
where b.b=a.column
);
这三种方法,习惯用第三种,不知道具体的执行效率是怎么样的,请高人指教。

 

本文地址:http://www.bhcode.net/article/20100607/10889.html


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM