在数据库的使用中排序和过滤也是经常的操作
>排序检索数据,关键字order
##1.按照某个列名排序
```
普通排序
mysql> select * from user;
+----+--------+-----+-----------------------+-----+
| id | name | age | address | sex |
+----+--------+-----+-----------------------+-----+
| 1 | 张三 | 20 | 北京海底市南区 | 1 |
| 2 | 李四 | 22 | 北京海底市南区 | 1 |
| 3 | 赵芸 | 32 | 上海市徐汇区 | 2 |
| 4 | 王丽 | 31 | 广州厦门 | 2 |
+----+--------+-----+-----------------------+-----+
4 rows in set (0.00 sec)
按照列名name排序是什么样呢?
mysql> select * from user order by name;
+----+--------+-----+-----------------------+-----+
| id | name | age | address | sex |
+----+--------+-----+-----------------------+-----+
| 1 | 张三 | 20 | 北京海底市南区 | 1 |
| 2 | 李四 | 22 | 北京海底市南区 | 1 |
| 4 | 王丽 | 31 | 广州厦门 | 2 |
| 3 | 赵芸 | 32 | 上海市徐汇区 | 2 |
+----+--------+-----+-----------------------+-----+
4 rows in set (0.00 sec)
```
##2.按照多个列名排序
```
mysql> select * from user order by name, age;
+----+--------+-----+-----------------------+-----+
| id | name | age | address | sex |
+----+--------+-----+-----------------------+-----+
| 1 | 张三 | 20 | 北京海底市南区 | 1 |
| 2 | 李四 | 22 | 北京海底市南区 | 1 |
| 4 | 王丽 | 31 | 广州厦门 | 2 |
| 3 | 赵芸 | 32 | 上海市徐汇区 | 2 |
+----+--------+-----+-----------------------+-----+
4 rows in set (0.00 sec)
```
##3.指定排序方向,默认为字母(a-z),升序
```
使用关键字desc,可以改为降序排列
mysql> select * from user order by name desc;
+----+--------+-----+-----------------------+-----+
| id | name | age | address | sex |
+----+--------+-----+-----------------------+-----+
| 3 | 赵芸 | 32 | 上海市徐汇区 | 2 |
| 4 | 王丽 | 31 | 广州厦门 | 2 |
| 2 | 李四 | 22 | 北京海底市南区 | 1 |
| 1 | 张三 | 20 | 北京海底市南区 | 1 |
+----+--------+-----+-----------------------+-----+
4 rows in set (0.00 sec)
```
##4.和limit配合使用,限制检索数据数量
```
mysql> select * from user order by name limit 3;
+----+--------+-----+-----------------------+-----+
| id | name | age | address | sex |
+----+--------+-----+-----------------------+-----+
| 1 | 张三 | 20 | 北京海底市南区 | 1 |
| 2 | 李四 | 22 | 北京海底市南区 | 1 |
| 4 | 王丽 | 31 | 广州厦门 | 2 |
+----+--------+-----+-----------------------+-----+
3 rows in set (0.00 sec)
```
> 数据过滤,关键字where
##1.检索某一条记录
```
mysql> select * from user where id = 2;
+----+--------+-----+-----------------------+-----+
| id | name | age | address | sex |
+----+--------+-----+-----------------------+-----+
| 2 | 李四 | 22 | 北京海底市南区 | 1 |
+----+--------+-----+-----------------------+-----+
1 row in set (0.00 sec)
和order by 配合使用
mysql> select * from user where id <4 order by name limit 3;
+----+--------+-----+-----------------------+-----+
| id | name | age | address | sex |
+----+--------+-----+-----------------------+-----+
| 1 | 张三 | 20 | 北京海底市南区 | 1 |
| 2 | 李四 | 22 | 北京海底市南区 | 1 |
| 3 | 赵芸 | 32 | 上海市徐汇区 | 2 |
+----+--------+-----+-----------------------+-----+
3 rows in set (0.00 sec)
关于where子句的位置: 在同时使用where和order by子句时候, 我们应该让order by位于where 子句之后。
```
##2.范围检索--between
```
mysql> select * from user where id between 2 and 4;
+----+--------+-----+-----------------------+-----+
| id | name | age | address | sex |
+----+--------+-----+-----------------------+-----+
| 2 | 李四 | 22 | 北京海底市南区 | 1 |
| 3 | 赵芸 | 32 | 上海市徐汇区 | 2 |
| 4 | 王丽 | 31 | 广州厦门 | 2 |
+----+--------+-----+-----------------------+-----+
3 rows in set (0.00 sec)
```
##3.过滤--组合where
```
mysql> select * from user where id >1 and id < 4;
+----+--------+-----+-----------------------+-----+
| id | name | age | address | sex |
+----+--------+-----+-----------------------+-----+
| 2 | 李四 | 22 | 北京海底市南区 | 1 |
| 3 | 赵芸 | 32 | 上海市徐汇区 | 2 |
+----+--------+-----+-----------------------+-----+
2 rows in set (0.00 sec)
```
## 4. 数据过滤--or 操作符
```
mysql> select * from user where id <2 or id >=3;
+----+--------+-----+-----------------------+-----+
| id | name | age | address | sex |
+----+--------+-----+-----------------------+-----+
| 1 | 张三 | 20 | 北京海底市南区 | 1 |
| 3 | 赵芸 | 32 | 上海市徐汇区 | 2 |
| 4 | 王丽 | 31 | 广州厦门 | 2 |
+----+--------+-----+-----------------------+-----+
3 rows in set (0.00 sec)
```
##5.数据过滤--in 操作符
```
in操作符可以用于指定操作范围,范围内每个条件都可以进行匹配。
mysql> select * from user where name in ("张三","李四");
+----+--------+-----+-----------------------+-----+
| id | name | age | address | sex |
+----+--------+-----+-----------------------+-----+
| 1 | 张三 | 20 | 北京海底市南区 | 1 |
| 2 | 李四 | 22 | 北京海底市南区 | 1 |
+----+--------+-----+-----------------------+-----+
2 rows in set (0.00 sec)
in操作符的优势:
1. 使用长的合法选项清单时候, in操作符比较直观。
2. in操作符计算的次序比较好管理
3. in操作符一般比or操作符效率快
4. in操作符可以包括其他select语句,能够更加动态的创建where子句
```
##6.数据过滤--not操作符
```
not操作符只有一个特点, 就是否定它后面的任何条件。
mysql支持not对in, between, exists子句取反。
mysql> select * from user where name not in ("张三","李四");
+----+--------+-----+--------------------+-----+
| id | name | age | address | sex |
+----+--------+-----+--------------------+-----+
| 3 | 赵芸 | 32 | 上海市徐汇区 | 2 |
| 4 | 王丽 | 31 | 广州厦门 | 2 |
+----+--------+-----+--------------------+-----+
2 rows in set (0.00 sec)
```