MySQL - 10查询结果排序与分页

排序的应用场景

已经掌握使用select语句结合where查询条件获取需要的数据,但在实际的应用中,还会遇到下面这类需求,又该如何解决?
学生按身高从高到低进行排列
双十一,某商城的商品交易量排行榜
博客中的文章按时间先后顺序显示

order by的使用

在SQL中,使用order by对查询结果集进行排序,可以按照一列或多列进行排序。

#order by语法
SELECT column_name1, column_name2
FROM table_name1, table_name2
ORDER BY column_name, column_name [ASC|DESC]

说明:

  1. ASC表示按升序排列,DESC表示按降序排列。
  2. 默认情况下,对列按升序排列。

limit的使用

在SELECT语句中使用LIMIT子句来约束要返回的记录数,通常使用LIMIT实现分页。

#limit语法
SELECT column_name1, column_name2 
FROM table_name1, table_name2 
LIMIT [offset,] row_count

说明:

  1. offset指定要返回的第一行的偏移量。第一行的偏移量是0,而不是1。
  2. row_count指定要返回的最大行数。

【经验分享】limit的分页公式:

limit (page-1)*row_count, row_count

实战案例:

使用下面的初始化数据

create table employee(
	id int not null auto_increment primary key,
	name varchar(30),
	sex varchar(1),
	salary int
);

insert into employee(name, sex, salary) values('lily0', '1', 5500);
insert into employee(name, sex, salary) values('lily1', '0', 4500);
insert into employee(name, sex, salary) values('lily2', '0', 4200);
insert into employee(name, sex, salary) values('lily3', '1', 7500);
insert into employee(name, sex, salary) values('lily4', '0', 8500);
insert into employee(name, sex, salary) values('lily5', '1', 6800);
insert into employee(name, sex, salary) values('lily6', '1', 12000);
insert into employee(name, sex, salary) values('lily7', '1', 3500);
insert into employee(name, sex, salary) values('lily8', '1', 6000);
insert into employee(name, sex, salary) values('lily9', '1', 8000);
insert into employee(name, sex, salary) values('lily10', '0', 10000);
insert into employee(name, sex, salary) values('lily11', '0', 4000);

order by的使用

mysql> select * from employee;
+----+--------+------+--------+
| id | name   | sex  | salary |
+----+--------+------+--------+
|  1 | lily0  | 1    |   5500 |
|  2 | lily1  | 0    |   4500 |
|  3 | lily2  | 0    |   4200 |
|  4 | lily3  | 1    |   7500 |
|  5 | lily4  | 0    |   8500 |
|  6 | lily5  | 1    |   6800 |
|  7 | lily6  | 1    |  12000 |
|  8 | lily7  | 1    |   3500 |
|  9 | lily8  | 1    |   6000 |
| 10 | lily9  | 1    |   8000 |
| 11 | lily10 | 0    |  10000 |
| 12 | lily11 | 0    |   4000 |
| 13 | gray   | 1    |   NULL |
+----+--------+------+--------+
13 rows in set (0.00 sec)

mysql> select * from employee order by salary;
+----+--------+------+--------+
| id | name   | sex  | salary |
+----+--------+------+--------+
| 13 | gray   | 1    |   NULL |
|  8 | lily7  | 1    |   3500 |
| 12 | lily11 | 0    |   4000 |
|  3 | lily2  | 0    |   4200 |
|  2 | lily1  | 0    |   4500 |
|  1 | lily0  | 1    |   5500 |
|  9 | lily8  | 1    |   6000 |
|  6 | lily5  | 1    |   6800 |
|  4 | lily3  | 1    |   7500 |
| 10 | lily9  | 1    |   8000 |
|  5 | lily4  | 0    |   8500 |
| 11 | lily10 | 0    |  10000 |
|  7 | lily6  | 1    |  12000 |
+----+--------+------+--------+
13 rows in set (0.00 sec)

mysql> select * from employee order by salary desc;
+----+--------+------+--------+
| id | name   | sex  | salary |
+----+--------+------+--------+
|  7 | lily6  | 1    |  12000 |
| 11 | lily10 | 0    |  10000 |
|  5 | lily4  | 0    |   8500 |
| 10 | lily9  | 1    |   8000 |
|  4 | lily3  | 1    |   7500 |
|  6 | lily5  | 1    |   6800 |
|  9 | lily8  | 1    |   6000 |
|  1 | lily0  | 1    |   5500 |
|  2 | lily1  | 0    |   4500 |
|  3 | lily2  | 0    |   4200 |
| 12 | lily11 | 0    |   4000 |
|  8 | lily7  | 1    |   3500 |
| 13 | gray   | 1    |   NULL |
+----+--------+------+--------+
13 rows in set (0.00 sec)

mysql> select * from employee order by sex, salary desc;
+----+--------+------+--------+
| id | name   | sex  | salary |
+----+--------+------+--------+
| 11 | lily10 | 0    |  10000 |
|  5 | lily4  | 0    |   8500 |
|  2 | lily1  | 0    |   4500 |
|  3 | lily2  | 0    |   4200 |
| 12 | lily11 | 0    |   4000 |
|  7 | lily6  | 1    |  12000 |
| 10 | lily9  | 1    |   8000 |
|  4 | lily3  | 1    |   7500 |
|  6 | lily5  | 1    |   6800 |
|  9 | lily8  | 1    |   6000 |
|  1 | lily0  | 1    |   5500 |
|  8 | lily7  | 1    |   3500 |
| 13 | gray   | 1    |   NULL |
+----+--------+------+--------+
13 rows in set (0.00 sec)

limit的使用

mysql> select * from employee limit 5;
+----+-------+------+--------+
| id | name  | sex  | salary |
+----+-------+------+--------+
|  1 | lily0 | 1    |   5500 |
|  2 | lily1 | 0    |   4500 |
|  3 | lily2 | 0    |   4200 |
|  4 | lily3 | 1    |   7500 |
|  5 | lily4 | 0    |   8500 |
+----+-------+------+--------+
5 rows in set (0.00 sec)

mysql> select * from employee limit 3;
+----+-------+------+--------+
| id | name  | sex  | salary |
+----+-------+------+--------+
|  1 | lily0 | 1    |   5500 |
|  2 | lily1 | 0    |   4500 |
|  3 | lily2 | 0    |   4200 |
+----+-------+------+--------+
3 rows in set (0.00 sec)

mysql> select * from employee limit 0, 5;
+----+-------+------+--------+
| id | name  | sex  | salary |
+----+-------+------+--------+
|  1 | lily0 | 1    |   5500 |
|  2 | lily1 | 0    |   4500 |
|  3 | lily2 | 0    |   4200 |
|  4 | lily3 | 1    |   7500 |
|  5 | lily4 | 0    |   8500 |
+----+-------+------+--------+
5 rows in set (0.00 sec)

mysql> select * from employee limit 5, 5;
+----+-------+------+--------+
| id | name  | sex  | salary |
+----+-------+------+--------+
|  6 | lily5 | 1    |   6800 |
|  7 | lily6 | 1    |  12000 |
|  8 | lily7 | 1    |   3500 |
|  9 | lily8 | 1    |   6000 |
| 10 | lily9 | 1    |   8000 |
+----+-------+------+--------+
5 rows in set (0.00 sec)

mysql> 

你可能感兴趣的:(MySQL教程)