MySql(五)select排序查询

基本语法:

SELECT 查询列表
FROM 表名
WHERE 查询条件
ORDER BY 排序列表(asc或desc)

特点:

  1. order by 子句可以支持单个字段、多个字段、表达式、函数、别名的查询
  2. order by 一般位置查询语句的最后面(limit子句除外)

举个栗子

/**查询员工信息,要求工资按照从低到高进行排序(默认升序)**/
SELECT * FROM employees ORDER BY salary ASC;
/**方法2:**/
SELECT * FROM employees ORDER BY salary;
/**查询部门标号大于等于90的员工信息,并且按照入职时间进行先后排序**/
SELECT * FROM employees 
WHERE department_id >= 90
ORDER BY hiredata ASC;
/**按照员工的年薪的高低显示员工的信息和年薪**/
SELECT *, salary*12(1+IFNULL(commission_pct,0) 年薪
FROM employees
ORDER BY 年薪 ASC;
/**按照姓名的长度显示员工的姓名和工资**/
SELECT LENGTH(last_name) 姓名字节长度 ,last_name,salary
FROM employees                  
ORDER BY 姓名字节长度;
/**查询员工信息,先按工资升序排序,在按照员工编号降序排序**/                
SELECT * FROM employees ORDER BY salary ASC,employee_id DESC;                   
                    

进阶练习:

/**选择工资不在8000-9000的员工的姓名和工资,按照工资的降序排序**/
SELECT last_name,salarty
FROM employees
WHERE salary NOT BETWEEN 8000 AND 9000
ORDER BY salary DESC;
/**查询邮箱中包含e的员工信息,先按照邮箱的字节数升序排序,在按照部门的降序排序**/
SELECT * 
FROM employees
WHERE email LIKE('%e%')
ORDER BY LENGTH(email) ASC, department_id DESC;

你可能感兴趣的:(MySql)