总结:
如果子查询返回的结果>1条,用到关键字:in;用比较符号会报错。
如果子查询返回的结果=1条,此时可以用 比较符号 : “=” “!=” “>" "<" “>=" "<="; 也可以用in。
如果子查询返回的结果=0条,此时用关键字:in;用比较符号不会报错,但是0条记录与任何数据比较的结果都是false 。
--SQL prepare
create global temporary table temp1 (
id number(3), desc1 char(5)
);
create global temporary table temp2 (id number(3), desc2 char(5));
insert into temp1 values (123, 'ABCDE');
insert into temp1 values (456, 'FGHIJ');
insert into temp2 values (456, 'ZZZZZ');
insert into temp2 values (789, 'MMMMM');
例子:
select * from temp1 where id != (select id from temp2 where id = 456)
456 FGHIJ
select * from temp1 where id != (select id from temp2 where id = 123)
none result
可以看出,如果子查询返回为空(0条记录)那么应该用in、not in。用比较符号不会报错,但是0条记录与任何数据比较的结果都是false 。
参考: