涉及到的问题
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—查询、排序的更多相关文章

  1. sql语句查询排序

    一:sql语句单词意义 order by 是用在where条件之后,用来对查询结果进行排序 order by 字段名 asc/desc asc 表示升序(默认为asc,可以省略) desc表示降序 o ...

  2. sql in查询排序

    1.默认下,使用select xxx where in(xx,xx)查询,返回结果是按主键排序的,如果要按in()中值的排列顺序,可以这样做:   select * from talbe where ...

  3. 《SQL CookBook 》笔记-第三章-多表查询

    目录 3.1 叠加两个行集 3.2 合并相关行 3.3 查找两个表中相同的行 3.4 查找只存在于一个表中的数据 3.5 从一个表检索与另一个表不相关的行 3.6 新增连接查询而不影响其他连接查询 3 ...

  4. SQL 提高查询效率

    1.关于SQL查询效率,100w数据,查询只要1秒,与您分享: 机器情况p4: 2.4内存: 1 Gos: windows 2003数据库: ms sql server 2000目的: 查询性能测试, ...

  5. SQL语句分组排序,多表关联排序

    SQL语句分组排序,多表关联排序总结几种常见的方法: 案例一: 在查询结果中按人数降序排列,若人数相同,则按课程号升序排列? 分析:单个表内的多个字段排序,一般可以直接用逗号分割实现. select ...

  6. 怎样用SQL语句查询一个数据库中的所有表?

    怎样用SQL语句查询一个数据库中的所有表?  --读取库中的所有表名 select name from sysobjects where xtype='u'--读取指定表的所有列名select nam ...

  7. 我如何调优SQL Server查询

    我是个懒人,我只想干尽可能少的活.当我干活的时候我不想太多.是,你没看错,这看起来很糟糕,作为一个DBA这很不合格.但在今天的文章里,我想给你展示下,当你想对特定查询创建索引设计时,你如何把你的工作和 ...

  8. SQL Server SQL分页查询

    SQL Server SQL分页查询的几种方式 目录 0.    序言 1.    TOP…NOT IN… 2.    ROW_NUMBER() 3.    OFFSET…FETCH 4.    执行 ...

  9. SQL分页查询结果不一致

    今天遇到了SQL分页查询结果不一致的情况,一看代码,原来是没加排序查询!!分页查询最好加排序,且以唯一性高的字段进行排序,如ID,时间等,以保持每页查询结果的准确! PS:又帮别人擦屁股!!

随机推荐

  1. [LeetCode题解]: Sort Colors

    前言   [LeetCode 题解]系列传送门:  http://www.cnblogs.com/double-win/category/573499.html   1.题目描述 Given an a ...

  2. java 添加到数据库的数据没有时分秒

    检查hibernate实体类的映射文件日期类型把date 改为 java.util.Date

  3. 利用html5 postMessage接口跨域设置iframe大小

    <!doctype html> <html> <head> <title>Document A</title> <meta chars ...

  4. 数独高阶技巧之八——SDC

    在本系列的第四篇“简单异数链”中,向大家介绍了XY-Wing等一系列Wing类技巧,并提到可以用(拐弯的)数组的观念来理解这些结构,经过第六篇ALS的学习之后,大家回过头再去看Wing,应该可以发现相 ...

  5. mongodb 搭建集群(分片+副本集)

    mongodb  搭建集群(分片+副本集) 一.搭建结构图: 二.搭建步骤:

  6. ArrayList用法详解

    1.什么是ArrayList ArrayList就是传说中的动态数组,用MSDN中的说法,就是Array的复杂版本,它提供了如下一些好处: 动态的增加和减少元素 实现了ICollection和ILis ...

  7. 洛谷P3613 睡觉困难综合征

    传送门 题解 人生第一道由乃…… 做这题之前应该先去把这一题给切掉->这里 我的题解->这里 然后先膜一波zsy大佬和flashhu大佬 大体思路就是先吧全0和全1的都跑答案,然后按位贪心 ...

  8. 条目二十二《切勿修改set或multiset的键》

    条目二十二<切勿修改set或multiset的键> 所有的标准关联容器一样,set和multiset按照一定的顺序来存放自己的元素,而这些容器的正确行为也是建立在其元素保持有序的基础之上的 ...

  9. leetcode-118-Pascal's Triangle(生成具有n行的帕斯卡三角形)

    题目描述: Given a non-negative integer numRows, generate the first numRows of Pascal's triangle. Example ...

  10. MyEclipse 汉化后切换回英文(中英文切换)

    没事玩玩MyEclipse,按网上的办法把它汉化了!搞了些教程看,教程用的都是英文,还是把MyEclipse也切换回原来的英文得了! 方法:1.复制MyEclipse的快捷方式:2.右键快捷方式-&g ...