子查询中的NULL问题
子查询返回有单行,多行和null值;适用于单行子查询的比较运算符是=,>,>=,<,<=<>和!=。适用于多行子查询的比较运算符是in,not in,any和any。在子查询中不可回避的是会返回null值,下面就这个问题来讨论一下。
SQL> select last_name,department_id from employees where department_id is null;
LAST_NAME DEPARTMENT_ID
------------------------- -------------
Grant
先来查看一下employees表中的department_id,它有个null值。
SQL> select last_name,department_id from employees
where department_id=(select department_id from employees where last_name='Haus');
no rows selected
在employees表中没有一行的last_name是等于Haus的,故子查询返回null值。由上面的查询知道department_id是有null值的,那没有显示呢?
SQL> select employee_id,manager_id from employees where manager_id is null;
EMPLOYEE_ID MANAGER_ID
----------- --- -------
100
先来查看一下employees表中的manager_id,它有个null值。
SQL> select last_name from employees where employee_id not in (select manager_id from
employees );
no rows selected
那这个为什么也没有显示?我们来先看一下in
SQL> select last_name from employees emp where emp.employee_id in (select manager_id from employees);
LAST_NAME
-------------------------
Cambrault
De Haan
Errazuriz
Fripp
这里用in可以显示了。这个原因是因为not in 和in本质上都是OR运算,但是计算逻辑OR时处理NULL的方式不同,产生的结果也不同。
select last_name from employees where employee_id not in (select manager_id from
employees );
子查询select manager_id from employees会产生很多值而且有一个NULL值,它们之间是OR的关系,假设manager_id是10,11和NULL,那么可以把上面的句子改为:
select last_name from employees where employee_id not in (10,11,null);
其中where employee_id not in (10,11,null)等价于where no (employee_id=10 or employee_id=11 or employee_id=null),拿employee_id=12来说明一下。
select last_name from employees emp where emp.employee_id in (select manager_id from employees);
按照上面的方法,假设manager_id是10,11和NULL,则句子改为:
select last_name from employees where employee_id not in (10,11,null);
其中where employee_id in (10,11,null)等价于where (employee_id=10 or employee_id=11 or employee_id=null),拿employee_id=10来说明一下。
子查询中的NULL问题的更多相关文章
- oracle 子查询中null的问题(in 和 not in)
这里的in后面的句子可以理解为or拼接,简单举例即 in (9566,9839,null)可以等价于mgr=9566 or mgr=9839 or mgr=null, not in (9566,983 ...
- SQLSERVER 子查询中使用ORDER BY
SQLSERVER如何在子查询中使用ORDER BY 今天在使用公司的一个pager接口的时候,需要传递一个查询的SQL语句,因为我希望他能够在pager对他查询出来的结果排序之前自己先进行排序, 于 ...
- 【杂记】mysql 左右连接查询中的NULL的数据筛选问题,查询NULL设置默认值,DATE_FORMAT函数
MySQL左右连接查询中的NULL的数据筛选问题 xpression 为 Null,则 IsNull 将返回 True:否则 IsNull 将返回 False. 如果 expression 由多个变量 ...
- SQLSERVER如何在子查询中使用ORDER BY
今天在使用公司的一个pager接口的时候,需要传递一个查询的SQL语句,因为我希望他能够在pager对他查询出来的结果排序之前自己先进行排序, 于是在这个SQL中添加了ORDER BY,但是得到的结果 ...
- Mybatis 一对多延迟加载,并且子查询中与主表字段不对应 (19)
Mybatis 一对多延迟加载,并且子查询中与主表字段不对应应用说明. 实现一对多关联(懒加载),一个教研组对应多个教师,既:教师的教研编号与教研组的教研编号关联,并且教师关联教研组外键与教研组编号 ...
- Mysql in子查询中加limit报错
Mysql in子查询中加limit报错 select id from aa where id in ( select id from bb limit 10 ); 改写成 SELECT id FRO ...
- Oracle子查询中any、some、all之间的区别
用some,any和all对子查询中返回的多行结果进行处理. 下面我们来简单介一下这几个关键词的含义. * Some在此表示满足其中一个的意义,是用or串起来的比较从句. * Any也表示满足其中一个 ...
- mysql更新子查询中的内容
UPDATE t_finance_certify_copy c SET c.biz_type=2,c.sub_biz_type=18WHERE c.finance_certify_id IN(SELE ...
- MySQL也真是让人郁闷(关于子查询中使用limit)
在MySQL4.1中子查询是不能使用LIMIT的,手册中也明确指明 “This version of MySQL doesn’t yet support ‘LIMIT & IN/ALL/ANY ...
随机推荐
- okhttp 简单用法
1.gradle 依赖 github 中查找最新的 2.MyApplication oncreate 中: @Override public void onCreate () { super.onCr ...
- 视差插件parallarx
github上的demo,自己拿来改了改. <!DOCTYPE html> <html> <head> <meta charset="UTF-8&q ...
- Windows Azure: Service Bus Relay
Service Host: using System; using System.Collections.Generic; using System.Linq; using System.Text; ...
- 说说JavaScript中的事件模型
1.javascript中为元素添加事件处理程序的方法有以下几种方式,可以为javascript元素添加事件处理程序 (1) 直接将事件处理代码写在html中(2) 定义一个函数,赋值给html元素的 ...
- bzoj 1044 [HAOI2008]木棍分割(二分+贪心,DP+优化)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1044 [题意] n根木棍拼到一起,最多可以切m刀,问切成后最大段的最小值及其方案数. ...
- sklearn进行拟合
# codind:utf-8 from sklearn.linear_model import SGDRegressor,LinearRegression,Ridge from sklearn.pre ...
- JS设计模式——10.门面模式
门面模式 这是一种组织性的模式,它可以用来修改类和对象的接口,使其更便于使用.它可以让程序员过得更轻松,使他们的代码变得更容易管理. 门面模式有两个作用: 简化类的接口 消除与使用她的客户代码之间的耦 ...
- popular short sentences
backward compatibility 向后兼容 archive 文档
- webpack4.5.0+vue2.5.16+vue-loader 实战组件化开发案例以及版本问题中踩的一些大坑!!!
一 vue-loader 我们先不管脚手架,只说vue-loader,简单讲就是可将.vue文件打包,实现组件化开发,即一个.vue文件就是一个组件,开发中只需要引入这个.vue组件就可以了! 然后. ...
- thinkphp报错Call to undefined method app\index\controller\Index::fetch()
因为要写一个系统,所以又重新下载了thinkphp,然后安装了一下.回忆起这个问题很容易让新手朋友费解.会出现如下报错:Call to undefined method app\index\contr ...