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:又帮别人擦屁股!!
随机推荐
- Android-ContentProvider原理图
ContentProvider的设计思想是模仿了Web里面的架构思想: Web服务器 对外暴露数据(提供被访问的地址Uri,并允许给客户端访问,也可以只让客户端访问某些行为) ContentPro ...
- MacOS安装使用Node.js
1. 到官网https://nodejs.org/zh-cn/download/下载,选择Macintosh Installer, 如下: 2. 按预设的下一步,Node.js版本为v6.10.0, ...
- 使用ContentPresenter,不使用ContentControl
参考: https://wpf.2000things.com/2017/04/06/1204-using-a-datatrigger-to-change-content-in-a-contentpre ...
- Windows store app[Part 1]:读取U盘数据
Windows 8系统下开发App程序,对于.NET程序员来说,需要重新熟悉下类库. 关于WinRT,引用一张网上传的很多的结构图: 图1 针对App的开发,App工作在系统划定的安全沙箱内,所以通过 ...
- robotframework+jenkins分布式执行自动化测试用例
http://blog.sina.com.cn/s/blog_53f023270101sc3w.html http://www.cnblogs.com/2test/p/5336842.html
- 为web文件夹添加IIS应用程序池用户权限
在文件夹或文件右键属性—>安全——>编辑——>添加——>输入IIS APPPOOL\应用程序池名,确定即可将IIS 7或7.5.8的应用程序池虚拟用户添加到权限控制里面,然后再 ...
- 动态添加select的option [转载]
动态给select标签添加option,结合前人经验以及自己经验,现在总结三种方法供大家参考,一起交流学习!首先是定义的select元素://根据ID获得select元素 var mySelect = ...
- 并发编程---线程 ;python中各种锁
一,概念 在传统操作系统中,每个进程有一个地址空间,而且默认就有一个控制线程 线程顾名思义,就是一条流水线工作的过程,一条流水线必须属于一个车间,一个车间的工作过程是一个进程 --车间负责把资源整合到 ...
- 各大SRC中的CSRF技巧
本文作者:i春秋签约作家——Max. 一.CSRF是什么? CSRF(Cross-site request forgery),中文名称:跨站请求伪造,也被称为:one click attack/ses ...
- VMware虚拟机中如何配置静态IP
我们首先说一下VMware的几个虚拟设备 VMnet0:用于虚拟桥接网络下的虚拟交换机 VMnet1:用于虚拟Host-Only网络下的虚拟交换机 VMnet8:用于虚拟NAT网络下的虚拟交换机 VM ...