Through the database data in descending order to achieve the effect of the latest data is displayed in front of the
- in descending order (the latest display in front of)
the SELECT * the FROM table name t ORDER BY t.uploadDatetime DESC;
format:
ORDER BY { column-Name | ColumnPosition | Expression }
[ ASC | DESC ]
[ NULLS FIRST | NULLS LAST ]
[ , column-Name | ColumnPosition | Expression
[ ASC | DESC ]
[ NULLS FIRST | NULLS LAST ]
] *
PS: The default is ascending order ASC
Knowledge points:
order by
You can take back the column number (number), column names, aliases, expressions, functions, grouped functionsorder by
Processing null values,DESC
null values preceding,ASC
null values after;order by
Clause may containselect
columns;- When use
select distinct
orgroup by
whenorder by
not using the column than SELECT; order by
Finally only put, you can not put the middle set of operations;- After the set of operations, do not take
order by
the time in a first column in ascending order (union all
exception); - Column named after the first set of operations
select
of the content,order by
only a selected firstselect
content to operate
select job, avg(sal) "Average Salary"
from emp
group by job
order by "Average Salary" DESC;
supplement:
Union (union all): the purpose of the directive is the result of two and a set of SQL statements together to get results you need.
Union: the results of the two sets and set operations, not including duplicates, while default sorting rule;
Union All: two result sets and set operations, including duplicates, do not sort;
The order by clause must be written at the end of a result set, and it will change the sort collation result of the operation. For the Union, Union All are valid.
Example:
1) Sort by Name (default ascending order), by name in ascending order (the ASC), descending by name (DESC), if the name in accordance with the same id in descending order (in descending order of the front corresponding to display the latest)
- Default ascending
the SELECT * the FROM t_test T the ORDER BY t.content;
- by name in ascending order
the SELECT * the FROM t_test T the ORDER BY t.content the ASC ;
- by name descending
the SELECT * the FROM t_test t the ORDER BY t.content DESC
- the same descending order by id (the latest ranking) name. Here I add a new data Test4
the SELECT * the FROM t_test t the ORDER BY t.content DESC , t.id DESC ;
2)缺省处理:oracle在order by时认为null是最大值, 所以如果是asc升序则排在最后, desc降序则排在最前.我们可以使用nulls first或者nulls last来控制null的位置。
-- 升序显示,默认null值在后面,使用nulls first将null显示在最前面
SELECT * FROM t_test t ORDER BY t.content ASC NULLS FIRST;
-- 降序显示,默认null值在前面,使用null last将null显示在最后面
SELECT * FROM t_test t ORDER BY t.content DESC NULLS FIRST;
mysql的如下:
-- null值显示在最前面
SELECT * FROM t_test t ORDER BY IF(ISNULL(t.content),0,1),t.content ASC;
-- null值显示在最后面
SELECT * FROM t_test t ORDER BY IF(ISNULL(t.content),1,0),t.content DESC;
3)将名称带有"test"的先显示,其余按照名称升序排序
在这里由于少了别名,出现报错,不过已解决
可以参考我的另一篇博客文章:https://www.cnblogs.com/HeiDi-BoKe/p/11763494.html
select * from t_test t1 where t1.content like '%test%'
Union all
select * from
(select * from t_test t2 where t2.content not like '%test%' order by t2.content asc) d;
上面的方法没有把null值显示出来。另一种方法也可实现,并显示null值
select * from t_test t
order by
case
when t.content like '%test%' then 0
else 1
end,t.content asc;
4)按照id为6的排到第一位,其余按照id降序排序
select * from t_test order by decode(id, 6,1), id desc;
PS:
DECODE函数的语法:DECODE(value,if1,then1,if2,then2,if3,then3,…,else);
DECODE函数说明:表示如果value等于if1时,DECODE函数的结果返回then1,…,如果不等于任何一个if值,则返回else。
sign函数语法:sign(n);
sign函数说明:取数字n的符号,大于0返回1,小于0返回-1,等于0返回0(n可以是表达式,(n-200))。
参考网址:https://blog.csdn.net/tian_tian2/article/details/80816275