MariaDB Where子句


在MariaDB中,WHERE子句與SELECTINSERTUPDATEDELETE語句一起使用來選擇或更改想要更改的滿足指定特定行記錄。

它是一個在表名後面出現的語句。

語法

[COMMAND] field,field2,... FROM table_name,table_name2,... WHERE [CONDITION]

注意WHERE子句是一個可選的子句。它可以和ANDORAND & ORLIKE運算子一起使用。

1. WHERE子句與單一條件

範例:

我們有一個表students,裡邊有一些資料。假設要查詢students表中的student_id小於6的所有記錄。

SELECT *   
FROM Students  
WHERE student_id < 6;

執行上面查詢語句,得到以下結果 -

MariaDB [testdb]> SELECT *
    -> FROM Students
    -> WHERE student_id < 6;
+------------+--------------+-----------------+----------------+
| student_id | student_name | student_address | admission_date |
+------------+--------------+-----------------+----------------+
|          1 | Maxsu        | Haikou          | 2017-01-07     |
|          3 | JMaster      | Beijing         | 2016-05-07     |
|          4 | Mahesh       | Guangzhou       | 2016-06-07     |
|          5 | Kobe         | Shanghai        | 2016-02-07     |
+------------+--------------+-----------------+----------------+
4 rows in set (0.00 sec)

2. WHERE子句與AND條件

為了方便演示,這裡再插入一條資料 -

INSERT INTO students  
(student_name, student_address, admission_date)  
VALUES('Maxsu','Sanya','2017-08-08 00:00:00');

當前students表中的全部資料是 -

MariaDB [testdb]> select * from students;
+------------+--------------+-----------------+----------------+
| student_id | student_name | student_address | admission_date |
+------------+--------------+-----------------+----------------+
|          1 | Maxsu        | Haikou          | 2017-01-07     |
|          3 | JMaster      | Beijing         | 2016-05-07     |
|          4 | Mahesh       | Guangzhou       | 2016-06-07     |
|          5 | Kobe         | Shanghai        | 2016-02-07     |
|          6 | Blaba        | Shengzhen       | 2016-08-07     |
|          7 | Maxsu        | Sanya           | 2017-08-08     |
+------------+--------------+-----------------+----------------+
6 rows in set (0.00 sec)

現在要查詢students表中的所有列,其中student_nameMaxsu,並且student_id大於1的行記錄。這兩個條件都必須滿足。參考以下查詢語句 -

SELECT *  
FROM Students  
WHERE student_name = 'Maxsu'  
AND student_id >1 ;

執行上面查詢語句,得到以下結果 -

MariaDB [testdb]> SELECT *
    -> FROM Students
    -> WHERE student_name = 'Maxsu'
    -> AND student_id >1 ;
+------------+--------------+-----------------+----------------+
| student_id | student_name | student_address | admission_date |
+------------+--------------+-----------------+----------------+
|          7 | Maxsu        | Sanya           | 2017-08-08     |
+------------+--------------+-----------------+----------------+
1 row in set (0.03 sec)

3. WHERE子句與OR條件

現在要查詢students表中的所有列,其中student_nameMaxsu,或者student_id大於5的行記錄。這兩個條件滿足其中一個即可。參考以下查詢語句 -

SELECT *  
FROM Students  
WHERE student_name = 'Maxsu'
OR student_id >5 ;

執行上面查詢語句,得到以下結果 -

MariaDB [testdb]> SELECT *
    -> FROM Students
    -> WHERE student_name = 'Maxsu'
    -> OR student_id >5 ;
+------------+--------------+-----------------+----------------+
| student_id | student_name | student_address | admission_date |
+------------+--------------+-----------------+----------------+
|          1 | Maxsu        | Haikou          | 2017-01-07     |
|          6 | Blaba        | Shengzhen       | 2016-08-07     |
|          7 | Maxsu        | Sanya           | 2017-08-08     |
+------------+--------------+-----------------+----------------+
3 rows in set (0.00 sec)

4. WHERE子句與OR/AND條件

假設要查詢students表中的所有列,那些符合student_nameAjeetstudent_id大於或等於1,或者student_addressShanghai的行記錄。

SELECT *  
FROM Students  
WHERE student_name = 'Maxsu'   
AND student_id >=1
OR (student_address = 'Shanghai');

執行上面查詢語句,得到以下結果 -

MariaDB [testdb]> SELECT *
    -> FROM Students
    -> WHERE student_name = 'Maxsu'
    -> AND student_id >=1
    -> OR (student_address = 'Shanghai');
+------------+--------------+-----------------+----------------+
| student_id | student_name | student_address | admission_date |
+------------+--------------+-----------------+----------------+
|          1 | Maxsu        | Haikou          | 2017-01-07     |
|          5 | Kobe         | Shanghai        | 2016-02-07     |
|          7 | Maxsu        | Sanya           | 2017-08-08     |
+------------+--------------+-----------------+----------------+
3 rows in set (0.00 sec)