mysql排序查询

一 查询格式

SELECT 查询列表

FROM 表名

[WHERE 查询条件]

ORDER BY 排序列表 ASC/DESC

二 例句

1. select * from employees where department_id>=90 order by hiredate; (按单个字段排序)

2. select *,salary*12*(1+IFNULL(commission_pct,0)) as '年薪' from employees order by salary*12*(1+IFNULL(commission_pct,0)); (按表达式排序)

3. select *,salary*12*(1+IFNULL(commission_pct,0)) as 年薪 from employees order by 年薪;(按别名排序,此处年薪不可带引号,否则排序结果不对)

4. select * ,LENGTH(last_name) as lengthofname from employees order by lengthofname DESC;(按别名排序)

5. select * from employees order by salary ASC,employee_id DESC;(按多个字段排序)

6. select * from employees where email like '%e%' order by LENGTH(email) desc,department_id asc; (按函数排序)

三 总结

1. ASC是升序,DESC是降序,默认是升序

2. ORDE BY子句可以支持单个字段,多个字段(有先后顺序),表达式,函数,别名的排序

3. ORDER BY子句一般放到查询语句的最后面,除了LIMIT子句外

 

相关文章:

  • 2021-11-27
  • 2022-12-23
  • 2021-12-24
  • 2021-09-22
  • 2021-12-19
  • 2021-05-26
  • 2022-01-27
猜你喜欢
  • 2022-01-07
  • 2021-11-18
  • 2021-12-14
  • 2022-03-05
  • 2021-12-09
  • 2021-12-27
  • 2022-01-04
相关资源
相似解决方案