MySQL必知必會---過濾數據

1.使用where子句
2.where子句操作符
2.1 檢查單個值
2.2 不匹配檢查
2.3 範圍值檢查
2.4 空值檢查

  1. 使用where子句
    數據庫表一般包含大量的數據,很少需要檢索表中的所有行。通常會根據特定操作或報告的需要提取表數據的子集。

例如:查找年齡等於22歲的行
MariaDB [test]> select age
-> from user
-> where age=22;
+------+
| age |
+------+
| 22 |
+------+
1 row in set (0.00 sec)

提示:在同時使用order by 和 where子句時,應該讓order by位於where之後。

  1. where子句操作符
    等於、不等於、小於、小於等於、大於、大於等於、在指定的兩個值之間使用between

2.1 檢查單個值
MariaDB [test]> select id,age,province
-> from user
-> where province = '北京';
+----+------+----------+
| id | age | province |
+----+------+----------+
| 1 | 22 | 北京 |
| 4 | 14 | 北京 |
| 7 | 45 | 北京 |
| 11 | 29 | 北京 |
| 13 | 24 | 北京 |
+----+------+----------+
5 rows in set (0.01 sec)

2.2 不匹配檢查

MariaDB [test]> select id, age, province
-> from user
-> where age <> 22;
+----+------+----------+
| id | age | province |
+----+------+----------+
| 2 | 25 | 廣東 |
| 3 | 56 | 天津 |
| 4 | 14 | 北京 |
| 5 | 36 | 廣東 |
| 6 | 68 | 湖南 |
| 7 | 45 | 北京 |
| 8 | 17 | 河北 |
| 9 | 33 | 天津 |
| 10 | 27 | 湖南 |
| 11 | 29 | 北京 |
| 12 | 70 | 廣東 |
| 13 | 24 | 北京 |
+----+------+----------+
12 rows in set (0.00 sec)

2.3 範圍值檢查

MariaDB [test]> select id,age,province
-> from user
-> where age between 25 and 33;
+----+------+----------+
| id | age | province |
+----+------+----------+
| 2 | 25 | 廣東 |
| 9 | 33 | 天津 |
| 10 | 27 | 湖南 |
| 11 | 29 | 北京 |
+----+------+----------+
4 rows in set (0.00 sec)

2.4 空值檢查

提示:空值NULL(no value)與0、空字符串或空格不同。

MariaDB [test]> select id,age,province
-> from user
-> where age IS NULL;
Empty set (0.00 sec)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章