练习:按照salary从高到低的顺序显示员工信息
升序:ASC (ascend) 降序:DESC (descend) 如果在ORDER BY 后没有显式指名排序的方式的话,则默认按照升序排列。
SELECT employee_id, last_name, salary
FROM employees
ORDER BY salary DESC;
使用列的别名,进行排序
列的别名只能在 ORDER BY 中使用,不能在WHERE中使用。
SELECT employee_id,salary,salary * 12 annual_sal
FROM employees
ORDER BY annual_sal;
强调格式:WHERE 需要声明在FROM后,ORDER BY之前。
SELECT employee_id,salary
FROM employees
WHERE department_id IN (50,60,70)
ORDER BY department_id DESC;
二级排序
显示员工信息,按照department_id的降序排列,salary的升序排列
SELECT employee_id,salary,department_id
FROM employees
ORDER BY department_id DESC,salary ASC;
分页
mysql使用limit实现数据的分页显示 需求1:每页显示20条记录,此时显示第1页
SELECT employee_id, last_name
FROM employees
LIMIT 0, 20;
需求:每页显示pageSize条记录,此时显示第pageNo页: 公式:LIMIT (pageNo-1) * pageSize,pageSize;
WHERE … ORDER BY …LIMIT 声明顺序如下:
SELECT employee_id,last_name,salary
FROM employees
WHERE salary > 6000
ORDER BY salary DESC
LIMIT 10;
查询员工的姓名和部门号和年薪,按年薪降序,按姓名升序显示
SELECT last_name, department_id, salary * (1 + IFNULL(commission_pct,0)) * 12 年薪
FROM employees
ORDER BY 年薪 DESC, last_name ASC;
选择工资不在 8000 到 17000 的员工的姓名和工资,按工资降序,显示第21到40位置的数据
SELECT last_name, salary
FROM employees
WHERE salary NOT BETWEEN 8000 AND 17000
ORDER BY salary DESC
LIMIT 20, 20;
查询邮箱中包含 e 的员工信息,并先按邮箱的字节数降序,再按部门号升序
SELECT *
FROM employees
WHERE email LIKE '%e%'
ORDER BY LENGTH(email) DESC, department_id ASC;
|