数据库_嵌套子查询_是用比较符号_还是用in

总结:

如果子查询返回的结果>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

 

参考:

Q: What would happen if the subquery returned multiple values?

A: Since the subquery in the example could return only a single value, it was acceptable for it to be written with the equals (=) operand. If multiple values are expected from the subquery, the in list operand should be used.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值