SQL Cookbook—查询、排序
涉及到的问题
1、在select语句中使用条件逻辑
2、限制返回的行数
3、从表中随机返回n条记录
4、将空值转换为实际值
5、对字母和数字混合的数据排序
6、处理排序空值
7、根据数据项的键排序
–8、从一个表中查找另一个表没有的值
–9、在一个表中查找与其他表不匹配的记录
–10、向查询中增加联接而不影响其他联接
–11、检测两个表中是否有相同的数据
–12、从多个表中返回丢失的数据
–13、在运算和比较时使用null值
–1、在select语句中使用条件逻辑
select ename,
sal,
case when sal<=2000 then 'UNDERPAID'
when sal>=4000 then 'OVERPAID'
else 'OK'
end as status
from emp
–2、限制返回的行数
select * from emp where rownum<=5
–3、从表中随机返回n条记录
select * from (
select ename, job from emp order by dbms_random.value()
)
where rownum <= 5
–4、将空值转换为实际值
–方法一
select comm, nvl(comm, 0) comm from emp
–方法二
select comm, coalesce(comm, 0) from emp
–方法三
select comm,
case when comm is null then 0
else comm
end comm
from emp
–5、对字母和数字混合的数据排序
–问题:现有字母和数字混合的数据,希望按照数字或字母部分来排序。考虑这个视图:
create view v_tt as
select ename||' '||deptno data from emp;
主要是通过replace和translate来实现
//by deptno(对数字排序)
select * from v_tt order by replace(data, replace(translate(data, '0123456789', '##########'), '#', ''), '');
//by ename(对字母排序)
select * from v_tt order by replace(translate(data, '0123456789', '##########'), '#', '');
补充:translate函数用法
select translate('123abc','2dc','4e') from dual;
因为from_string和to_string的位置是一一对应的,2对应4,d对应e,c没有对应的值,所以c应该会被删除。所以例子的字符里的2会替换为4,d因为字符串里没有,所以不作替换,c由于没有对应的替换字符,所以字符串里的c会被删除,那么可以得出,结果是143ab
–6、处理排序空值
主要方法是通过使用CASE表达式来“标记”一个值是否为NULL。这里标记有两个值,一个表示NULL,一个表示非NULL。这样,只要在ORDER BY子句中增加标记列,便可以很容易的控制空值是排在前面还是排在后面,而不会被空值所干扰。
//非空值按升序排序,空值排最后
select ename,sal,comm from(
select ename,sal,comm,
case when comm is null then 0 else 1 end as is_null
from emp
) x order by is_null desc,comm
//非空值按降序排序,空值排最后
select ename,sal,comm from(
select ename,sal,comm,
case when comm is null then 0 else 1 end as is_null
from emp
) x order by is_null desc,comm desc
//非空值按升序排序,空值排最前面
select ename,sal,comm from(
select ename,sal,comm,
case when comm is null then 0 else 1 end as is_null
from emp
) x order by is_null,comm
//非空值按降序排序,空值排最前面
select ename,sal,comm from(
select ename,sal,comm,
case when comm is null then 0 else 1 end as is_null
from emp
) x order by is_null,comm desc
在ORACLE中还可以使用NULLS FIRST和NULLS LAST来实现如上功能
//非空值按升序排序,空值排最后
select ename,sal,comm
from emp
order by comm nulls last
//非空值按降序排序,空值排最后
select ename,sal,comm
from emp
order by comm desc nulls last
//非空值按升序排序,空值排最前面
select ename,sal,comm
from emp
order by comm nulls first
//非空值按降序排序,空值排最前面
select ename,sal,comm
from emp
order by comm desc nulls first
–7、根据数据项的键排序
要根据某些条件逻辑来排序。例如,如果JOB是SALESMAN,要根据COMM来排序。否则,根据SAL排序。
select ename,sal,job,comm from emp
order by case when job ='SALSEMAN' then comm else sal end
–8、从一个表中查找另一个表没有的值
问题:从表dept中查找在表emp中不存在的数据的所有部门。示例数据中deptno的值在emp中不存在。
方法一:
select deptno from dept
minus
select deptno from emp
方法二:
select deptno from dept where deptno not in (select deptno from emp where deptno is not null)
注意:
1)、oracle中not in如果返回的有null值的话,不会返回记录。
例如:select deptno from dept where deptno not in (10, 20, null)
2)、在sql中,true or null的结果是true,而false or null的结果是null,所以在使用in和or计算时,值可能是null的情况,这一点要记住。
要解决not in这样问题,可以使用not exists和相关子查询(推荐)
select deptno from dept d where not exists(select 'xx' from emp e where e.deptno = d.deptno)
–9、在一个表中查找与其他表不匹配的记录
问题:对于具有相同关键字的两个表,要在一个表中查找与另一个表中不匹配的行。例如,要查找没有职员的部门(emp为从表)。
select d.* from emp e, dept d where e.deptno(+)=d.deptno and e.deptno is null
–10、向查询中增加联接而不影响其他联接
例如,要获得所有的员工信息、他们的工作部门的地点以及所获得的奖励
select e.ename, d.loc, eb.received
from emp e, dept d, emp_bonus eb
where e.deptno = d.deptno and e.empno = eb.empno
这样的查询结果,如果员工没有奖金,则无法显示该员工的信息,那么,无论有无奖金都要显示员工信息,就要使用到外连接
select e.ename, d.loc, eb.received
from emp e, dept d, emp_bonus eb
where e.deptno = d.deptno and e.empno = eb.empno(+)
order by 2
–11、检测两个表中是否有相同的数据
create view v2
as select * from scott.emp where deptno!=10
union all
select * from scott.emp where ename=upper('ward')
原理:
1)、首先,查找出表emp中存在而视图v2中没有的行。
2)、然后,合并在视图v2中存在,而在表emp中没有的行。
(select empno,ename,job,mgr,hiredate,sal,comm,deptno,count(*) as cnt
from v2
group by empno,ename,job,mgr,hiredate,sal,comm,deptno
minus
select empno,ename,job,mgr,hiredate,sal,comm,deptno,count(*) as cnt
from scott.emp
group by empno,ename,job,mgr,hiredate,sal,comm,deptno)
union all
(select empno,ename,job,mgr,hiredate,sal,comm,deptno,count(*) as cnt
from scott.emp
group by empno,ename,job,mgr,hiredate,sal,comm,deptno
minus
select empno,ename,job,mgr,hiredate,sal,comm,deptno,count(*) as cnt
from v2
group by empno,ename,job,mgr,hiredate,sal,comm,deptno)
–12、从多个表中返回丢失的数据
select d.deptno,d.dname,e.ename from scott.dept d,scott.emp e
where d.deptno=e.deptno(+)
union
select d.deptno,d.dname,e.ename from scott.dept d,scott.emp e
where d.deptno(+)=e.deptno
–13、在运算和比较时使用null值
select ename, comm from scott.emp where coalesce(comm,0) < (select comm from scott.emp where ename=upper('ward'));
SQL Cookbook—查询、排序的更多相关文章
- sql语句查询排序
一:sql语句单词意义 order by 是用在where条件之后,用来对查询结果进行排序 order by 字段名 asc/desc asc 表示升序(默认为asc,可以省略) desc表示降序 o ...
- sql in查询排序
1.默认下,使用select xxx where in(xx,xx)查询,返回结果是按主键排序的,如果要按in()中值的排列顺序,可以这样做: select * from talbe where ...
- 《SQL CookBook 》笔记-第三章-多表查询
目录 3.1 叠加两个行集 3.2 合并相关行 3.3 查找两个表中相同的行 3.4 查找只存在于一个表中的数据 3.5 从一个表检索与另一个表不相关的行 3.6 新增连接查询而不影响其他连接查询 3 ...
- SQL 提高查询效率
1.关于SQL查询效率,100w数据,查询只要1秒,与您分享: 机器情况p4: 2.4内存: 1 Gos: windows 2003数据库: ms sql server 2000目的: 查询性能测试, ...
- SQL语句分组排序,多表关联排序
SQL语句分组排序,多表关联排序总结几种常见的方法: 案例一: 在查询结果中按人数降序排列,若人数相同,则按课程号升序排列? 分析:单个表内的多个字段排序,一般可以直接用逗号分割实现. select ...
- 怎样用SQL语句查询一个数据库中的所有表?
怎样用SQL语句查询一个数据库中的所有表? --读取库中的所有表名 select name from sysobjects where xtype='u'--读取指定表的所有列名select nam ...
- 我如何调优SQL Server查询
我是个懒人,我只想干尽可能少的活.当我干活的时候我不想太多.是,你没看错,这看起来很糟糕,作为一个DBA这很不合格.但在今天的文章里,我想给你展示下,当你想对特定查询创建索引设计时,你如何把你的工作和 ...
- SQL Server SQL分页查询
SQL Server SQL分页查询的几种方式 目录 0. 序言 1. TOP…NOT IN… 2. ROW_NUMBER() 3. OFFSET…FETCH 4. 执行 ...
- SQL分页查询结果不一致
今天遇到了SQL分页查询结果不一致的情况,一看代码,原来是没加排序查询!!分页查询最好加排序,且以唯一性高的字段进行排序,如ID,时间等,以保持每页查询结果的准确! PS:又帮别人擦屁股!!
随机推荐
- [Postgres]关于Postgres的INHERIT,分表
实在是很强大的功能 可以通过Check的制约把结构相同的表合并起来,或者反过来说,可以在一个表名下数据库自动的根据Check条件往对应的分表里存储数据 [USER_DATA表] CREATE TABL ...
- 【装饰者模式】Decorator Pattern
装饰者模式,这个模式说我一直记忆深刻的模式,因为Java的IO,我以前总觉得Java的IO是一个类爆炸,自从明白了装饰者模式,Java的IO体系让我觉得非常的可爱,我们现在看看什么是装饰者,然后再来看 ...
- js学习日记-对象字面量
一.对象字面量语法 var person={ name:'小王', age:18, _pri:233 } 成员名称的单引号不是必须的 最后一个成员结尾不要用逗号,不然在某些浏览器中会抛出错误 成员名相 ...
- 【python】@property装饰器
Python内置的@property装饰器可以把类的方法伪装成属性调用的方式.也就是本来是Foo.func()的调用方法,变成Foo.func的方式.在很多场合下,这是一种非常有用的机制. class ...
- js webstrom中svn的配置及使用
js webstorm中svn的配置及使用 一.webstorm配置svn: 1.在webstorm工具中找到file(文件)-setting(设置)菜单按钮: 2.在左边菜单中找到plus(插件) ...
- 三个数组求中位数,并且求最后中位数的中位数-----C++算法实现
文件Median.h #include <list> class CMedian { public: explicit CMedian(); virtual ~CMedian(); voi ...
- Linux环境下动态链接库的生成和使用
使用自己封装的so时遇到了点问题,本着简便原则决定写个demo看看,顺便记录下整个过程. 1)生成so所需的文件如下: print.h #ifndef __print_h__ #define __pr ...
- API自动化测试 Soap UI工具介绍
一. 建立测试用例 (一) 基本概念 soapUI 中工程的层次结构 项目名称:位于最上层 (BookStoreTest),项目可以包含多个服务的定义. REST 服务定义:服务其实是对多个 ...
- Redis数据持久化,安全
一.redis数据持久化 由于redis是一个内存数据库,如果系统遇到致命问题需要关机或重启,内存中的数据就会丢失,这是生产环境所不能允许的.所以redis提供了数据持久化的能力. redis提供了两 ...
- iOS 图片的存储以及读取和删除
将图片存储到本地 NSArray *dirArray = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask ...