ORDER BY 是用来对select结果排序的,很简单,但是也总结一下吧。
1. 按列名排序,默认是升序的。
单列升序:select<column_name> from <table_name> order by <column_name>;
单列降序:select <column_name> from table_name order by <column_name> desc;
多列升序:select <column_one>, <column_two> from table_name order by <column_one>, <column_two>;
多列降序:select <column_one>, <column_two> from <table_name> order by <column_one> desc, <column_two> desc;
多列混合降序:select column_one, column_two from table_name order by column_one desc, column_two desc;
2. 按列的位序排序, 默认是升序
select * from <table_name> order by 1
3. NULL排序
空值在前:select <column_name> from <table_name> order by <column_name> NULLS FIRST;
空值在后:select <column_name> from <table_name> order by <column_name> NULLS LAST;
1. 按列名排序,默认是升序的。
单列升序:select<column_name> from <table_name> order by <column_name>;
单列降序:select <column_name> from table_name order by <column_name> desc;
多列升序:select <column_one>, <column_two> from table_name order by <column_one>, <column_two>;
多列降序:select <column_one>, <column_two> from <table_name> order by <column_one> desc, <column_two> desc;
多列混合降序:select column_one, column_two from table_name order by column_one desc, column_two desc;
2. 按列的位序排序, 默认是升序
select * from <table_name> order by 1
3. NULL排序
空值在前:select <column_name> from <table_name> order by <column_name> NULLS FIRST;
空值在后:select <column_name> from <table_name> order by <column_name> NULLS LAST;