排序查询

1.仅仅排序

order by语句一般放在语句的最后面,进行限制

  • ASC默认升序查询

  • DESC降序查询

SELECT 
  * 
FROM
  `pmw_goodstype` 

ORDER BY id ASC;

2.排序筛选

SELECT 
  * 
FROM
  `pmw_goodstype` 

 WHERE `parentid` >1

ORDER BY id DESC;

按照表达式进行排序

  • 通过salary进行表达式的计算

  • order By支持别名

SELECT 
  id ,salary*12 AS year_salary
FROM
  `pmw_goodstype` 

 WHERE `parentid` >1

ORDER BY year_salary2 DESC;

3.多个排序条件

  • 先出现的排序字段,优先级更高一些;这里当id字段相同时,按照year_salary进行降序排序

SELECT 
  id ,salary*12 AS year_salary
FROM
  `pmw_goodstype` 

 WHERE `parentid` >1

ORDER BY year_salary DESC, id ASC;

Last updated