已经掌握使用select语句结合where查询条件获取需要的数据,但在实际的应用中,还会遇到下面这类需求,又该如何解决?
学生按身高从高到低进行排列
双十一,某商城的商品交易量排行榜
博客中的文章按时间先后顺序显示
在SQL中,使用order by对查询结果集进行排序,可以按照一列或多列进行排序。
#order by语法
SELECT column_name1, column_name2
FROM table_name1, table_name2
ORDER BY column_name, column_name [ASC|DESC]
说明:
在SELECT语句中使用LIMIT子句来约束要返回的记录数,通常使用LIMIT实现分页。
#limit语法
SELECT column_name1, column_name2
FROM table_name1, table_name2
LIMIT [offset,] 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);
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)
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>