MySQL學習筆記之三排序和過濾

在數據庫的使用中排序和過濾也是常常的操做mysql

排序檢索數據,關鍵字ordergit

##1.按照某個列名排序github

普通排序
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.按照多個列名排序sql

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)

數據過濾,關鍵字wherecode

##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.範圍檢索--betweenget

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.過濾--組合whereit

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)

個人網站:https://wayne214.github.io

相關文章
相關標籤/搜索