一个sql查询排序小技巧

如果你想做分段统计比如

SELECT CASE WHEN (num = 0) THEN '0' WHEN (age > 0 AND age <= 10) THEN '10'
WHEN (age > 100 AND age <=200) THEN '100' ELSE '>200' END as num

这样的情况,如果用别名排序的话,数据库字符串排序会变成乱序,导致难以浏览,所以可以在别名上加前缀
,这样就可以通过首字母排序了,比如



SELECT CASE WHEN (num = 0) THEN 'a. 0' WHEN (age > 0 AND age <= 10) THEN 'b. 10'
WHEN (age > 100 AND age <=200) THEN 'c. 100' ELSE 'd. >200' END as num

这样select出来的数据就会按照你设想的方式排序了