SELECT employee_id,first_name,salary FROM employees ORDER BY salary; SELECT employee_id,first_name,salary FROM employees ORDER BY salary desc;
只根据一个字段进行排序
SELECT last_name,job_id,department_id,hire_date FROM employees ORDER BY hire_date; SELECT last_name,job_id,department_id,hire_date FROM employees ORDER BY hire_date DESC; SELECT employee_id,last_name,salary*12 annsal FROM employees ORDER BY annsal;
ASC可省略
SELECT last_name,department_id,salary FROM employees ORDER BY department_id ASC,salary DESC;
LIMIT n,m
#前10条记录 SELECT * FROM employees LIMIT 0,10; #或者 SELECT * FROM employees LIMIT 10; #第11至第20条记录 SELECT * FROM employees LIMIT 10,10; #第21至第30条记录 SELECT * FROM employees LIMIT 20,10;
MySQL8.0中可以使用"LIMIT 3 OFFSET 4",表示从第5条记录开始后面的3条记录,跟"LIMIT 4,3" 效果相同
分页显示公式:(pageIndex-1)*pageSize,pageSize
SELECT * FROM table LIMIT (pageIndex-1)*pageSize,pageSize;
LIMIT字句必须放在整个SELECT语句的最后。
不同的数据库中使用的关键字不同,MySQL中使用的是LIMIT
SELECT TOP 5 name,hp_max FROM heros ORDER BY hp_max DESC;
SELECT name,hp_max FROM heros ORDER BY hp_max DESC FETCH FIRST 5 ROWS ONLY;
SELECT rownum, last_name,salary FROM employees WHERE rownum<5 ORDER BY salary DESC;
取出排序后的数据
SELECT rownum,last_name,salary FROM( SELECT last_name,salary FROM employees ORDER BY salary DESC) WHERE rownum<10;