sql查询in包含null的情况

sql查询in包含null的情况下,查询结果为空。

解决方案:1.用EXISTS 替换

SELECT d.*
FROM dept d
where NOT EXISTS (
SELECT NULL 
FROM emp e
WHERE e.DEPTNO=d.DEPTNO

2.查询子句去除掉null

SELECT * FROM dept d
WHERE d.DEPTNO NOT in (SELECT DEPTNO FROM new_dept WHERE DEPTNO is NOT NULL);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
《精通SQ:结构化查询语言详解》全面讲解SQL语言,提供317个典型应用,读者可以随查随用,针对SQL Server和Oracle进行讲解,很有代表性。 全书共包括大小实例317个,突出了速学速查的特色。《精通SQ:结构化查询语言详解》内容丰富,讲解通俗易懂,具有很强的实用性和可操作性。 目录 第1章 数据库与SQL基础  1.1 数据库的基本概念  1.1.1 数据库的由来  1.1.2 数据库系统的概念  1.2 数据库系统的结构、组成及工作流程 1.2.1 数据库的体系结构  1.2.2 数据库系统的组成  1.2.3 数据库的工作流程  1.3 数据库的发展  1.3.1 第一代数据库  1.3.2 第二代数据库  1.3.3 新一代数据库技术的研究和发展  1.4 关系数据库  1.4.1 关系模型  1.4.2 Codd十二法则  1.4.3 范式  1.5 SQL语言基础  1.5.1 SQL的历史  1.5.2 SQL语言的组成 1.5.3 SQL语句的结构  1.5.4 SQL的优点  1.5.5 SQL的执行  1.6 SQL环境  1.6.1 环境  1.6.2 SQL的层次结构  1.6.3 客户程序和服务程序系统  1.6.4 SQL环境中对象的命名规则  第2章 主要的关系数据库与SQL  2.1 SQL Server  2.1.1 SQL Server的结构  2.1.2 数据库访问标准化接口-ODBC  2.1.3 使用查询分析器执行SQL语句  2.2 Transact-SQL  2.2.1 Transact-SQL 概述  2.2.2 Transact-SQL的主要组成  2.2.3 Transact-SQL的一些重要命令  2.3 Oracle数据库  2.3.1 Oracle数据库软件组成 2.3.2 Oracle数据库体系结构  2.3.3 Oracle数据库系统结构 2.3.4 使用SQL*Plus执行SQL语句 2.4 PL/SQL简介  2.4.1 PL/SQL的特点  2.4.2 PL/SQL程序结构 第3章 创建、修改和删除表 3.1 表的基础知识  3.1.1 表的基本结构  3.1.2 表的种类  3.2 SQL数据类型  3.2.1 字符型数据 3.2.2 数字型数据  3.2.3 日期数据类型 3.2.4 二进制数据类型 3.2.5 文本和图形数据类型  3.2.6 自定义数据类型  3.3 表的创建(CREATE)  3.3.1 创建基本表  3.3.2 非空约束  3.3.3 DEFAULT指定缺省值  3.4 表的修改  3.4.1 增加新列 3.4.2 删除列  3.4.3 修改列  3.5 表的删除与重命名  3.5.1 重命名表  3.5.2 删除表  3.6 创建、删除数据库  3.6.1 数据库的创建  3.6.2 SQL Server中数据库的创建 3.6.3 删除数据库  第4章 索引与视图的创建 4.1 索引的基础知识 4.1.1 索引的概念  4.1.2 索引的结构  4.2 索引的创建与销毁  4.2.1 基本创建语法  4.2.2 本章实例用到的实例表 4.2.3 创建简单的非簇索引 4.2.4 多字段非簇索引的创建  4.2.5 使用UNIQUE关键字创建惟一索引  4.2.6 使用CLUSTERDE关键字创建簇索引 4.2.7 索引的销毁  4.2.8 使用索引的几点原则  4.3 视图的基础知识  4.3.1 视图简介  4.3.2 视图的优缺点  4.4 视图的创建与销毁  4.4.1 基本创建语法  4.4.2 创建简单的视图  4.4.3 利用视图简化表的复杂连接  4.4.4 利用视图简化复杂查询 4.4.5 视图的销毁  4.4.6 使用视图的几点原则  第5章 简单的查询  5.1 查询的基本结构  5.1.1 SELECT语句的结构  5.1.2 SELECT语句的执行步骤  5.2 列的查询  5.2.1 本章用到的实例表  5.2.2 单列查询  5.2.3 使用DISTINCT去除重复信息  5.2.4 多列查询  5.2.5 查询所有的列 5.3 排序查询结果 5.3.1 单列排序  5.3.2 多列排序 5.3.3 采用序号进行多列排序 5.3.4 反向排序  5.4 使用WHERE子句定义搜索条件查询  5.4.1 WHERE子句单条件查询 5.4.2 单值比较运算符 5.4.3 BETWEEN运算符范围筛选 5.4.4 NULL值的判断 第6章 复杂搜索条件查询  6.1 本章用到的实例表  6.2 组合查询条件  6.2.1 AND运算符  6.2.2 OR运算符  6.2.3 AND、OR运算符的组合使用  6.3 IN运算符  6.3.1 IN运算符的使用  6.3.2 IN运算符与OR运算符  6.4 NOT运算符 6.4.1 使用NOT运算符  6.4.2 NOT运算符与运算符  6.5 使用LIKE进行模糊查询 6.5.1 LIKE运算符  6.5.2 “%”通配符 6.5.3 “_”通配符 6.5.4 “[]”通配符  6.5.5 使用ESCAPE定义转义符  第7章 连接符、数值运算与函数  7.1 本章实例用到的表 7.2 连接符 7.2.1 连接符的应用  7.2.2 使用别名 7.3 数值运算  7.3.1 数学运算符的种类 7.3.2 数学运算符的运用  7.3.3 使用CAST表达式转换数据类型  7.3.4 使用CASE表达式  7.4 函数  7.4.1 有关函数的说明  7.4.2 字符处理函数  7.4.3 算术运算函数  7.4.4 日期时间函数  7.4.5 CONVERT()函数转换日期、时间 第8章 聚合分析与分组  8.1 聚合分析的基本概念  8.1.1 聚合分析  8.1.2 聚合函数  8.2 聚合函数的应用  8.2.1 求和函数-SUM()  8.2.2 计数函数-COUNT()  8.2.3 最大/最小值函数-MAX()/MIN()  8.2.4 均值函数-AVG()  8.2.5 聚合分析的重值处理  8.2.6 聚合函数的组合使用  8.3 组合查询  8.3.1 GROUP BY子句创建分组  8.3.2 GROUP BY子句根据多列组合行  8.3.3 ROLLUP运算符和CUBE运算符  8.3.4 GROUP BY子句中的NULL值处理  8.3.5 HAVING子句  8.3.6 HAVING子句与WHERE子句  8.3.7 SELECT语句各查询子句总结  第9章 多表查询  9.1 本章用到的实例表  9.2 表的基本连接  9.2.1 连接表的目的 9.2.2 简单的二表连接 9.2.3 多表连接 9.2.4 使用表别名 9.2.5 采用JOIN关键字建立连接 9.3 表的连接类型  9.3.1 自连接  9.3.2 自然连接(NATURAL JOIN)  9.3.3 内连接(INNER JOIN) 9.3.4 外连接(OUTER JOIN)  9.3.5 交叉连接(CROSS JOIN)  9.4 UNION与UNION JOIN  9.4.1 关系的集合运算  9.4.2 UNION运算符  9.4.3 ORDER BY子句排序UNION运算结果  9.4.4 对多表进行UNION运算  9.4.5 UNION JOIN 连接表  9.5 表连接的其他应用及注意问题  9.5.1 连接表进行聚合运算  9.5.2 多表连接的综合运用  9.5.3 多表连接注意事项  第10章 子查询  10.1 创建和使用返回单值的子查询  10.1.1 在多表查询中使用子查询  10.1.2 在子查询中使用聚合函数  10.2 创建和使用返回多行的子查询  10.2.1 IN子查询  10.2.2 IN子查询实现集合交和集合差运算 10.2.3 EXISTS子查询  10.2.4 EXISTS子查询实现两表交集  10.2.5 SOME/ALL子查询  10.2.6 UNIQUE子查询  10.3 相关子查询  10.3.1 使用IN引入相关子查询  10.3.2 比较运算符引入相关子查询 10.3.3 在HAVING子句中使用相关子查询  10.4 嵌套子查询  10.5 使用子查询创建视图  10.6 树查询 第11章 数据插入操作  11.1 插入单行记录  11.1.1 基本语法  11.1.2 整行插入  11.1.3 NULL值的插入 11.1.4 惟一值的插入  11.1.5 特定字段数据插入  11.1.6 通过视图插入行 11.2 插入多行记录 11.2.1 由VALUES关键字引入多行数据插入  11.2.2 使用SELECT语句插入值 11.3 表中数据的复制  11.3.1 基本语法  11.3.2 应用实例  11.4 从外部数据源导入、导出数据  11.4.1 Access数据库数据的导出  11.4.2 Access数据库数据的导入  11.4.3 SQL Server数据库数据导出  11.4.4 SQL Server数据库数据导入  第12章 数据的更新和删除  12.1 更新表中的数据  12.1.1 UPDATE语句的基本语法 12.1.2 UPDATE语句更新列值  12.1.3 利用子查询更新多行的值 12.1.4 依据外表值更新数据 12.1.5 分步更新表 12.2 删除表中的数据  12.2.1 DELETE语句基本语法  12.2.2 DELETE语句删除单行数据  12.2.3 DELETE语句删除多行数据  12.2.4 DELETE语句删除所有行  12.2.5 TRUNCATE TABLE语句 12.3 通过视图更新表  12.3.1 可更新视图的约束  12.3.2 通过视图更新表数据  12.3.3 通过视图删除表数据  第13章 安全性控制  13.1 SQL安全模式  13.1.1 授权ID  13.1.2 SQL的安全对象和权限 13.1.3 授权图  13.2 角色管理  13.2.1 CREATE语句创建角色  13.2.2 DROP语句删除角色  13.2.3 GRANT语句授予角色  13.2.4 REVOKE语句取消角色  13.3 权限管理  13.3.1 GRANT语句授予权限  13.3.2 REVOKE语句取消权限 13.3.3 SELECT权限控制 13.3.4 INSERT权限控制  13.3.5 UPDATE权限控制 13.3.6 DELETE权限控制  13.4 SQL Server安全管理  13.4.1 SQL Server登录认证 13.4.2 SQL Server用户ID的管理 13.4.3 SQL Server权限管理  13.4.4 SQL Server角色管理  13.5 Oracle安全管理  13.5.1 Oracle中用户、资源、概要文件、模式的概念 13.5.2 Oracle中的用户管理  13.5.3 Oracle中的资源管理  13.5.4 Oracle中的权限管理 13.5.5 Oracle中的角色管理  第14章 完整性控制 14.1 完整性约束简介  14.1.1 数据的完整性  14.1.2 完整性约束的类型  14.2 与表有关的约束  14.2.1 列约束与表约束的创建  14.2.2 NOT NULL(非空)约束 14.2.3 UNIQUE(惟一)约束  14.2.4 PRIMARY KEY(主键)约束 14.2.5 FOREIGN KEY(外键)约束 14.2.6 CHECK(校验)约束 14.3 深入探讨外键与完整性检查  14.3.1 引用完整性检查  14.3.2 MATCH子句  14.3.3 更新、删除操作规则 14.4 域约束与断言 14.4.1 域与域约束 14.4.2 利用断言创建多表约束 14.5 SQL Server中的完整性控制  14.5.1 创建规则(Rule) 14.5.2 规则的绑定与松绑 14.5.3 创建缺省值(Default)  14.5.4 缺省值的绑定与松绑  第15章 存储过程与函数  15.1 SQL中的存储过程与函数  15.2 SQL Server 中的流控制语句  15.2.1 BEGIN...END语句 15.2.2 IF...ELSE语句  15.2.3 WHILE、BREAK和CONTINUE语句 15.2.4 DECLARE语句 15.2.5 GOTO label语句 15.2.6 RETURN语句  15.2.7 WAITFOR语句  15.2.8 PRINT语句  15.2.9 注释 15.3 SQL Server中的存储过程和函数  15.3.1 系统存储过程  15.3.2 使用CREATE PROCEDURE创建存储过程 15.3.3 使用EXECUTE语句调用存储过程  15.3.4 使用CREATE FUNCTION创建函数  15.3.5 使用Enterprise Manager创建存储过程和函数 15.3.6 修改和删除存储过程和函数  15.4 Oracle中的流控制语句  15.4.1 条件语句  15.4.2 循环语句  15.4.3 标号和GOTO  15.5 Oracle数据库中的存储过程  15.5.1 存储过程的创建与调用  15.5.2 Oracle中存储过程和函数的管理 第16章 SQL触发器  16.1 触发器的基本概念  16.1.1 触发器简介  16.1.2 触发器执行环境 16.2 SQL Server中的触发器  16.2.1 SQL Server触发器的种类  16.2.2 使用CREATE TRIGGER命令创建触发器 16.2.3 INSERT触发器  16.2.4 DELETE触发器  16.2.5 UPDATE触发器  16.2.6 INSTEAD OF触发器  16.2.7 嵌套触发器  16.2.8 递归触发器  16.2.9 SQL Server中触发器的管理  16.3 Oracle数据库中触发器的操作  16.3.1 Oracle触发器类型  16.3.2 触发器的创建 16.3.3 创建系统触发器  16.3.4 触发器的触发次序和触发谓词的使用  16.3.5 Oracle触发器的管理  第17章 SQL中游标的使用  17.1 SQL游标的基本概念  17.1.1 游标的概念  17.1.2 游标的作用及其应用 17.2 SQL游标的使用 17.2.1 使用DECLARE CURSOR语句创建游标  17.2.2 使用OPEN/CLOSE语句打开/关闭游标  17.2.3 使用FETCH语句检索数据  17.2.4 基于游标的定位DELETE语句  17.2.5 基于游标的定位UPDATE语句 17.3 SQL Server中游标的扩展  17.3.1 Transact_SQL扩展DECLARE CURSOR语法  17.3.2 @@CURSOR_ROWS全局变量确定游标的行数  17.3.3 @@FETCH_STATUS全局变量检测FETCH操作的状态 17.3.4 游标的关闭与释放 17.3.5 游标变量  17.3.6 使用系统过程管理游标  17.4 Oracle中游标的使用  17.4.1 显式游标与隐式游标 17.4.2 游标的属性  17.4.3 %TYPE、%ROWTYPE定义记录变量  17.4.4 参数化游标  17.4.5 游标中的循环  17.4.6 游标变量 17.5 小结  第18章 事务控制与并发处理 18.1 SQL事务控制  18.1.1 事务控制的引入  18.1.2 事务的特性  18.1.3 SQL中与事务有关的语句  18.2 事务控制的具体实现 18.2.1 开始事务  18.2.2 SET CONSTRAINTS语句设置约束的延期执行  18.2.3 终止事务  18.3 并发控制 18.3.1 并发操作的问题 18.3.2 事务隔离级别  18.3.3 SET TRANSACTION设置事务属性  18.4 SQL Server中的并发事务控制  18.4.1 锁的分类  18.4.2 SQL Server中表级锁的使用  18.4.3 设置隔离级别实现并发控制 18.4.4 死锁及其预防  18.5 Oracle中的并发事务控制  18.5.1 通过加锁避免写数据丢失 18.5.2 设置只读事务(READ ONLY)  18.5.3 Oracle中的隔离级别  第19章 嵌入式SQL  19.1 SQL的调用  19.1.1 直接调用SQL 19.1.2 嵌入式SQL  19.1.3 SQL调用层接口(CLI)  19.2 嵌入式SQL的使用  19.2.1 创建嵌入式SQL语句  19.2.2 SQL通信区  19.2.3 主变量  19.2.4 嵌入式SQL中使用游标  19.3 检索、操作SQL数据  19.3.1 不需要游标的SQL DML操作 19.3.2 使用游标的SQL DML操作 19.3.3 动态SQL技术  19.4 SQL Server中嵌入式SQL的编译运行 19.4.1 嵌入式SQL代码  19.4.2 预编译文件  19.4.3 设置Visual C++ 6.0连接  19.4.4 编译运行程序  19.5 Oracle中嵌入式SQL的编译运行  19.5.1 嵌入式SQL代码 19.5.2 预编译文件  19.5.3 设置Visual C++ 6.0编译环境  19.5.4 编译运行程序  附录A SQL保留字  附录B 常用的SQL命令  附录C 关于运行环境的说明  C.1 SQL Server 2000  C.1.1 直接访问  C.1.2 从企业管理器访问 C.2 Oracle系统
--高级查询在数据库中用得是最频繁的,也是应用最广泛的。 Ø 基本常用查询 --select select * from student; --all 查询所有 select all sex from student; --distinct 过滤重复 select distinct sex from student; --count 统计 select count(*) from student; select count(sex) from student; select count(distinct sex) from student; --top 取前N条记录 select top 3 * from student; --alias column name 列重命名 select id as 编号, name '名称', sex 性别 from student; --alias table name 表重命名 select id, name, s.id, s.name from student s; --column 列运算 select (age + id) col from student; select s.name + '-' + c.name from classes c, student s where s.cid = c.id; --where 条件 select * from student where id = 2; select * from student where id > 7; select * from student where id < 3; select * from student where id <> 3; select * from student where id >= 3; select * from student where id <= 5; select * from student where id !> 3; select * from student where id !< 5; --and 并且 select * from student where id > 2 and sex = 1; --or 或者 select * from student where id = 2 or sex = 1; --between ... and ... 相当于并且 select * from student where id between 2 and 5; select * from student where id not between 2 and 5; --like 模糊查询 select * from student where name like '%a%'; select * from student where name like '%[a][o]%'; select * from student where name not like '%a%'; select * from student where name like 'ja%'; select * from student where name not like '%[j,n]%'; select * from student where name like '%[j,n,a]%'; select * from student where name like '%[^ja,as,on]%'; select * from student where name like '%[ja_on]%'; --in 子查询 select * from student where id in (1, 2); --not in 不在其中 select * from student where id not in (1, 2); --is null 是空 select * from student where age is null; --is not null 不为空 select * from student where age is not null; --order by 排序 select * from student order by name; select * from student order by name desc; select * from student order by name asc; --group by 分组 按照年龄进行分组统计 select count(age), age from student group by age; 按照性别进行分组统计 select count(*), sex from student group by sex; 按照年龄和性别组合分组统计,并排序 select count(*), sex from student group by sex, age order by age; 按照性别分组,并且是id大于2的记录最后按照性别排序 select count(*), sex from student where id > 2 group by sex order by sex; 查询id大于2的数据,并完成运算后的结果进行分组和排序 select count(*), (sex * id) new from student where id > 2 group by sex * id order by sex * id; --group by all 所有分组 按照年龄分组,是所有的年龄 select count(*), age from student group by all age; --having 分组过滤条件 按照年龄分组,过滤年龄为空的数据,并且统计分组的条数和现实年龄信息 select count(*), age from student group by age having age is not null; 按照年龄和cid组合分组,过滤条件是cid大于1的记录 select count(*), cid, sex from student group by cid, sex having cid > 1; 按照年龄分组,过滤条件是分组后的记录条数大于等于2 select count(*), age from student group by age having count(age) >= 2; 按照cid和性别组合分组,过滤条件是cid大于1,cid的最大值大于2 select count(*), cid, sex from student group by cid, sex having cid > 1 and max(cid) > 2; Ø 嵌套子查询查询是一个嵌套在select、insert、update或delete语句或其他子查询中的查询。任何允许使用表达式的地方都可以使用子查询。子查询也称为内部查询或内部选择,而包含查询的语句也成为外部查询或外部选择。 # from (select … table)示例 将一个table的查询结果当做一个新表进行查询 select * from ( select id, name from student where sex = 1 ) t where t.id > 2; 上面括号中的语句,就是子查询语句(内部查询)。在外面的是外部查询,其中外部查询可以包含以下语句: 1、 包含常规选择列表组件的常规select查询 2、 包含一个或多个表或视图名称的常规from语句 3、 可选的where子句 4、 可选的group by子句 5、 可选的having子句 # 示例 查询班级信息,统计班级学生人生 select *, (select count(*) from student where cid = classes.id) as num from classes order by num; # in, not in子句查询示例 查询班级id大于小于的这些班级的学生信息 select * from student where cid in ( select id from classes where id > 2 and id < 4 ); 查询不是班的学生信息 select * from student where cid not in ( select id from classes where name = '2班' ) in、not in 后面的子句返回的结果必须是一列,这一列的结果将会作为查询条件对应前面的条件。如cid对应子句的id; # exists和not exists子句查询示例 查询存在班级id为的学生信息 select * from student where exists ( select * from classes where id = student.cid and id = 3 ); 查询没有分配班级的学生信息 select * from student where not exists ( select * from classes where id = student.cid ); exists和not exists查询需要内部查询和外部查询进行一个关联的条件,如果没有这个条件将是查询到的所有信息。如:id等于student.id; # some、any、all子句查询示例 查询班级的学生年龄大于班级的学生的年龄的信息 select * from student where cid = 5 and age > all ( select age from student where cid = 3 ); select * from student where cid = 5 and age > any ( select age from student where cid = 3 ); select * from student where cid = 5 and age > some ( select age from student where cid = 3 ); Ø 聚合查询 1、 distinct去掉重复数据 select distinct sex from student; select count(sex), count(distinct sex) from student; 2、 compute和compute by汇总查询 对年龄大于的进行汇总 select age from student where age > 20 order by age compute sum(age) by age; 对年龄大于的按照性别进行分组汇总年龄信息 select id, sex, age from student where age > 20 order by sex, age compute sum(age) by sex; 按照年龄分组汇总 select age from student where age > 20 order by age, id compute sum(age); 按照年龄分组,年龄汇总,id找最大值 select id, age from student where age > 20 order by age compute sum(age), max(id); compute进行汇总前面是查询的结果,后面一条结果集就是汇总的信息。compute子句中可以添加多个汇总表达式,可以添加的信息如下: a、 可选by关键字。它是每一列计算指定的行聚合 b、 行聚合函数名称。包括sum、avg、min、max、count等 c、 要对其执行聚合函数的列 compute by适合做先分组后汇总的业务。compute by后面的列一定要是order by中出现的列。 3、 cube汇总 cube汇总和compute效果类似,但语法较简洁,而且返回的是一个结果集。 select count(*), sex from student group by sex with cube; select count(*), age, sum(age) from student where age is not null group by age with cube; cube要结合group by语句完成分组汇总 Ø 排序函数 排序在很多地方需要用到,需要对查询结果进行排序并且给出序号。比如: 1、 对某张表进行排序,序号需要递增不重复的 2、 对学生的成绩进行排序,得出名次,名次可以并列,但名次的序号是连续递增的 3、 在某些排序的情况下,需要跳空序号,虽然是并列 基本语法 排序函数 over([分组语句] 排序子句[desc][asc]) 排序子句 order by 列名, 列名 分组子句 partition by 分组列, 分组列 # row_number函数 根据排序子句给出递增连续序号 按照名称排序的顺序递增 select s.id, s.name, cid, c.name, row_number() over(order by c.name) as number from student s, classes c where cid = c.id; # rank函数函数 根据排序子句给出递增的序号,但是存在并列并且跳空 顺序递增 select id, name, rank() over(order by cid) as rank from student; 跳过相同递增 select s.id, s.name, cid, c.name, rank() over(order by c.name) as rank from student s, classes c where cid = c.id; # dense_rank函数 根据排序子句给出递增的序号,但是存在并列不跳空 不跳过,直接递增 select s.id, s.name, cid, c.name, dense_rank() over(order by c.name) as dense from student s, classes c where cid = c.id; # partition by分组子句 可以完成对分组的数据进行增加排序,partition by可以与以上三个函数联合使用。 利用partition by按照班级名称分组,学生id排序 select s.id, s.name, cid, c.name, row_number() over(partition by c.name order by s.id) as rank from student s, classes c where cid = c.id; select s.id, s.name, cid, c.name, rank() over(partition by c.name order by s.id) as rank from student s, classes c where cid = c.id; select s.id, s.name, cid, c.name, dense_rank() over(partition by c.name order by s.id) as rank from student s, classes c where cid = c.id; # ntile平均排序函数 将要排序的数据进行平分,然后按照等分排序。ntile中的参数代表分成多少等分。 select s.id, s.name, cid, c.name, ntile(5) over(order by c.name) as ntile from student s, classes c where cid = c.id; Ø 集合运算 操作两组查询结果,进行交集、并集、减集运算 1、 union和union all进行并集运算 --union 并集、不重复 select id, name from student where name like 'ja%' union select id, name from student where id = 4; --并集、重复 select * from student where name like 'ja%' union all select * from student; 2、 intersect进行交集运算 --交集(相同部分) select * from student where name like 'ja%' intersect select * from student; 3、 except进行减集运算 --减集(除相同部分) select * from student where name like 'ja%' except select * from student where name like 'jas%'; Ø 公式表表达式 查询表的时候,有时候中间表需要重复使用,这些子查询被重复查询调用,不但效率低,而且可读性低,不利于理解。那么公式表表达式可以解决这个问题。 我们可以将公式表表达式(CET)视为临时结果集,在select、insert、update、delete或是create view语句的执行范围内进行定义。 --表达式 with statNum(id, num) as ( select cid, count(*) from student where id > 0 group by cid ) select id, num from statNum order by id; with statNum(id, num) as ( select cid, count(*) from student where id > 0 group by cid ) select max(id), avg(num) from statNum; Ø 连接查询 1、 简化连接查询 --简化联接查询 select s.id, s.name, c.id, c.name from student s, classes c where s.cid = c.id; 2、 left join左连接 --左连接 select s.id, s.name, c.id, c.name from student s left join classes c on s.cid = c.id; 3、 right join右连接 --右连接 select s.id, s.name, c.id, c.name from student s right join classes c on s.cid = c.id; 4、 inner join内连接 --内连接 select s.id, s.name, c.id, c.name from student s inner join classes c on s.cid = c.id; --inner可以省略 select s.id, s.name, c.id, c.name from student s join classes c on s.cid = c.id; 5、 cross join交叉连接 --交叉联接查询,结果是一个笛卡儿乘积 select s.id, s.name, c.id, c.name from student s cross join classes c --where s.cid = c.id; 6、 自连接(同一张表进行连接查询) --自连接 select distinct s.* from student s, student s1 where s.id <> s1.id and s.sex = s1.sex; Ø 函数 1、 聚合函数 max最大值、min最小值、count统计、avg平均值、sum求和、var求方差 select max(age) max_age, min(age) min_age, count(age) count_age, avg(age) avg_age, sum(age) sum_age, var(age) var_age from student; 2、 日期时间函数 select dateAdd(day, 3, getDate());--加天 select dateAdd(year, 3, getDate());--加年 select dateAdd(hour, 3, getDate());--加小时 --返回跨两个指定日期的日期边界数和时间边界数 select dateDiff(day, '2011-06-20', getDate()); --相差秒数 select dateDiff(second, '2011-06-22 11:00:00', getDate()); --相差小时数 select dateDiff(hour, '2011-06-22 10:00:00', getDate()); select dateName(month, getDate());--当前月份 select dateName(minute, getDate());--当前分钟 select dateName(weekday, getDate());--当前星期 select datePart(month, getDate());--当前月份 select datePart(weekday, getDate());--当前星期 select datePart(second, getDate());--当前秒数 select day(getDate());--返回当前日期天数 select day('2011-06-30');--返回当前日期天数 select month(getDate());--返回当前日期月份 select month('2011-11-10'); select year(getDate());--返回当前日期年份 select year('2010-11-10'); select getDate();--当前系统日期 select getUTCDate();--utc日期 3、 数学函数 select pi();--PI函数 select rand(100), rand(50), rand(), rand();--随机数 select round(rand(), 3), round(rand(100), 5);--精确小数位 --精确位数,负数表示小数点前 select round(123.456, 2), round(254.124, -2); select round(123.4567, 1, 2);

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值