如何进行SQL排序
order by 语法如下:
SELECT "栏位名"
FROM "表格名"
[WHERE "条件"]
ORDER BY "栏位名" [ASC, DESC];
[ ] 代表 where 是一定需要的。不过,如果 where 子句存在的话,它是在 order by子句之前,asc表结果会以由小往大的顺序列出,而 desc 代表结果会以由大往小的顺序列出。如果两者皆没有被写出的话,那我们就会用asc。
1几种排序的写法:
单列升序:select name from student order by name; (默认升序,即使不写asc)
单列降序:select name from student order by name desc;
多列升序:select id, name from student order by id, name;
多列混合排序:select id, name from student order by id desc, name asc; (先按id降序,如果id相同则按name升序)
2.1 select * from article where nameid=1 order by arid; //查找文章表(article),根据 用户编号(nameid)order by 排序 文章编号(rid) 默认升序(asc);
2.2 如果需要从最后面看,使用降序(desc),
select * from article where nameid=1 order by arid desc;
2.3 select * from table where a like '%b%' order by 字段 desc/asc; //
2.4 将null放在最前:select * from student order by name asc nulls first;
将null放在最后:select * from student order by name desc nulls last;
2.5 强制某列排第一
select * from student order by decode(id, 3, 1, 2), id; // id为3的值排第一