涉及到的问题
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. Xamarin.Forms中 Navigation,NavigationPage详解

    1.Xamarin Forms下有四个成员类:Element,VisualElement,Page,NavigationPage 基类为Element,继承的子类分别是VisualElement,Pa ...

  2. ZKEACMS 配置使用 HTTPS

    在开始之前,请升级你的ZKEACMS到最新版本,旧版本使用HTTPS会有问题 https加密链接,在访问的过程中,可以保护你的隐私,保证你的敏感数据不会被别人偷窥,窃取.如果你的服务器在境外,使用ht ...

  3. django drf viewsets和routers

    1.定义VIew from django.shortcuts import render from rest_framework.views import APIView from rest_fram ...

  4. C#设置图片透明度

    逐个像素进行Alpha值的设置,网上其他的代码不能处理有透明背景的图片,因此要对Alpha.R.G.B均为0的透明色进行特殊处理,不做转换. private Bitmap SetImageOpacit ...

  5. plsql连接oracle客户端(简单,实用方案)附件

    plsql 连接 oracle 需要在本地安装oracle客户端(附件中下载): 将文件下载下来后,放到任意目录,例如 D:\instantclient_10_2 修改 tnsnames.ora 文件 ...

  6. HTML5 开发APP 第一章

    当今天下,移动端基本上是安卓和苹果的天下,基本上没微软什么事,作为微软忠实的支持者,也要顺势而变. 但安卓和IOS  开发是两个完全不同的世界,有没有一种技术,开发完以后可以运行在任意终端呢,答案是有 ...

  7. QTP如何准确识别Dialog中的对象

    QTP脚本中有一个点击网页弹出框确定按钮的操作,实际运行时发现存在问题:调试过程,可正常识别并点击:但批量运行时不能识别并点击的概率接近100%. 修改WinButton的其中一个对象属性后,该问题解 ...

  8. NOI2007 社交网络

    题目链接:戳我 就是在floyd计算dis的时候,顺便把两点之间最短路的个数也计算了qwqwq \(sum[i][j]=\sum sum[i][k]*sum[k][j]\) 代码如下: #includ ...

  9. StratifiedShuffleSplit()函数 实现对数据集的划分

    sklearn.model_selection.StratifiedShuffleSplit(n_splits=10, test_size=’default’, train_size=None, ra ...

  10. react-dnd使用介绍

    核心API 想要灵活使用,就先知道几个核心API DragSource 用于包装你需要拖动的组件,使组件能够被拖拽(make it draggable) DropTarget 用于包装接收拖拽元素的组 ...