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_nullG

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

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

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

order by从句使用case

原文地址:https://www.cnblogs.com/zcy-backend/p/6805784.html