搬山行者

无志愁压头,有志能搬山

业余程序员的学习笔记~


Sql经典实例 第二章

目录

依据字串排序

select ename, job  
  from emp 
  order by substr(job,length(job)-2, 2)

依据数值排序

select ename, sal 
  from emp 
  order by sal

依据数值排序,并限制结果集的行数

select ename, sal 
  from emp 
  order by sal 
  limit 5

对含有字母和数字的字段进行排序

-- order by ename 本质上是提取字符串
select data from
(select ename || '' || deptno as data 
  from emp) emp
order by replace(translate(data,'0123456789','##########), '#’', '')  

排序时对null的处理

select ename, sal, comm
from (select ename, sal, comm, case when comm is null then 0 else comm end as is_null
  from emp) x
order by is_null desc, comm 

动态调整排序项

select ename, sal, comm
    from emp
    order by case when job = 'salesman' then comm else sal end