例子: Select * from table1 A where A.col1 not in ( select col1 from table2 B ) 如果这样,本来应该有一条数据,结果没有。 如果我改写成这样: Select * from table1 A where A.col1 not in ( select col1 from table2 B where B.col2 like '%XXX%') 结果就正确,有一条数据显示。 是不是 Oracle not in 跟的子查询的结果集不能大于多少行阿? 诚心请教!
首先搞清楚( select col1 from table2 B ) 的结果集大于( select col1 from table2 B where B.col2 like '%XXX%') 的结果集 其次你用的是not in 而不是 in,结果集越大符合的记录越少。所以 你的出的结果是正确的。
那你改成not exists试试看 Select * from table1 A where not exists( select col1 from table2 B )
我解释的不够明确,我的(select col1 from table2 B) 和 ( select col1 from table2 B where B.col2 like '%XXX%') 子句,只是在结果集的大小上有区别。比如 col1 是 'A0001' 而 实际上 table2的 col1 根本就没有'A0001'的值,全是'B%'。 所以按道理两个sql出来的结果应该一样,但是如果子句的数据量过大,not in 就没有效果了!
ghtghtmalone,你好,对于 exists 我理解的是说如果子句有任何纪录,那么就是1,否则就是 0 ,所有的结果集都不能出来, 我理解的应该不够对,你能给解释以下吗? 比如Select * from table1 A where not exists( select col1 from table2 B ) 能等于Select * from table1 A where A.col1 not in ( select col1 from table2 B ) 吗? 用not in,你要制定那个列去和查询的子句去比较,用not exists 怎么考虑?
select col1 from table2 B 这个结果是不是有null值? 如果结果集里面有null值,那么这个not in就不会返回任何记录 select col1 from table2 B where col1 is null ; 执行上面语句是不是有结果返回
Select * from table1 A where A.col1 not in ( select col1 from table2 B )
如果这样,本来应该有一条数据,结果没有。
如果我改写成这样:
Select * from table1 A where A.col1 not in ( select col1 from table2 B where B.col2 like '%XXX%')
结果就正确,有一条数据显示。
是不是 Oracle not in 跟的子查询的结果集不能大于多少行阿?
诚心请教!
其次你用的是not in 而不是 in,结果集越大符合的记录越少。所以
你的出的结果是正确的。
Select * from table1 A where not exists( select col1 from table2 B )
子句,只是在结果集的大小上有区别。比如 col1 是 'A0001' 而 实际上 table2的 col1 根本就没有'A0001'的值,全是'B%'。
所以按道理两个sql出来的结果应该一样,但是如果子句的数据量过大,not in 就没有效果了!
我理解的应该不够对,你能给解释以下吗?
比如Select * from table1 A where not exists( select col1 from table2 B ) 能等于Select * from table1 A where A.col1 not in ( select col1 from table2 B )
吗? 用not in,你要制定那个列去和查询的子句去比较,用not exists 怎么考虑?
这个结果是不是有null值?
如果结果集里面有null值,那么这个not in就不会返回任何记录
select col1 from table2 B where col1 is null ;
执行上面语句是不是有结果返回