MySQL必知必會---過濾資料

來源:互聯網
上載者:User

標籤: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、Null 字元串或空格不同。

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

MySQL必知必會---過濾資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.