SQL Cookbook:查询结果排序

1、查询结果排序

order by子句中可以使用select中没出现的列,但如果查询中使用group by或者distinct,则不能使用未出现的列。

 

2、按子串排序

1 select * from film order by substring(title, length(title) - 2)

起始位置是title的倒数第三个字符,mysql中字符串下标从1开始

 

3、处理排序空值

1 select * from 
2 (select title, case when rating is null then 0 else 1 end as is_null from film) x 
3 order by is_null\G

可以使用嵌套查询+case语句的方法

 

4、根据数据项的值,使用不同的排序逻辑

1 select * from film order by case when length < 70 then title else film_id end\G

order by从句使用case

 

posted on 2017-05-04 10:47  一个后端狗  阅读(155)  评论(0编辑  收藏  举报

导航