WHERE <查詢條件> {<判定運算1>,<判定運算2>,…}
其中,判定運算其結果取值為 TRUE、FALSE 和 UNKNOWN。{=|<|<=|>|>=|<=>|<>|!=}
<表示式2>[NOT]LIKE
<表示式2>[NOT][REGEXP|RLIKE]
<表示式2>[NOT]BETWEEN
<表示式2>AND
<表示式3>IS[NOT]NULL
mysql> use test_db Database changed mysql> SELECT name,height -> FROM tb_students_info -> WHERE height=170; +-------+--------+ | name | height | +-------+--------+ | Susan | 170 | +-------+--------+ 1 row in set (0.17 sec)該語句採用了簡單的相等過濾,查詢一個指定列 height 的具體值 170。
mysql> SELECT name,age -> FROM tb_students_info -> WHERE age<22; +------+------+ | name | age | +------+------+ | John | 21 | +------+------+ 1 row in set (0.05 sec)可以看到,查詢結果中所有記錄的 age 欄位的值均小於 22 歲,而大於或等於 22 歲的記錄沒有被返回。
mysql> SELECT * FROM tb_students_info -> WHERE age>21 AND height>=175; +----+--------+---------+------+------+--------+------------+ | id | name | dept_id | age | sex | height | login_date | +----+--------+---------+------+------+--------+------------+ | 3 | Henry | 2 | 23 | M | 185 | 2015-05-31 | | 5 | Jim | 1 | 24 | M | 175 | 2016-01-15 | | 9 | Thomas | 3 | 22 | M | 178 | 2016-06-07 | +----+--------+---------+------+------+--------+------------+ 3 rows in set (0.06 sec)
注意:上例的 WHERE 子句中只包含一個 AND 語句,把兩個過濾條件組合在一起,實際上可以新增多個 AND 過濾條件,增加條件的同時增加一個 AND 關鍵字。
<表示式1> [NOT] LIKE <表示式2>
字串匹配是一種模式匹配,使用運算子 LIKE 設定過濾條件,過濾條件使用萬用字元進行匹配運算,而不是判斷是否相等進行比較。【範例 4】在 tb_students_info 表中,查詢所有以“T”字母開頭的學生姓名,輸入的 SQL 的語句和執行結果如下所示。注意:不要過度使用萬用字元,對萬用字元檢索的處理一般會比其他檢索方式花費更長的時間。
mysql> SELECT name FROM tb_students_info -> WHERE name LIKE 'T%'; +--------+ | name | +--------+ | Thomas | | Tom | +--------+ 2 rows in set (0.12 sec)
【範例 5】在 tb_students_info 表中,查詢所有包含“e”字母的學生姓名,輸入的 SQL 的語句和執行結果如下所示。注意:在搜尋匹配時,萬用字元“%”可以放在不同位置。
mysql> SELECT name FROM tb_students_info -> WHERE name LIKE '%e%'; +-------+ | name | +-------+ | Green | | Henry | | Jane | +-------+ 3 rows in set (0.00 sec)由執行結果可以看出,該語句查詢字串中包含字母 e 的學生的姓名,只要名字中有字母 e,其前面或後面無論有多少個字元,都滿足查詢的條件。
mysql> SELECT name FROM tb_students_info -> WHERE name LIKE '____y'; +-------+ | name | +-------+ | Henry | +-------+ 1 row in set (0.00 sec)
mysql> SELECT * FROM tb_students_info -> WHERE login_date<'2016-01-01'; +----+-------+---------+------+------+--------+------------+ | id | name | dept_id | age | sex | height | login_date | +----+-------+---------+------+------+--------+------------+ | 1 | Dany | 1 | 25 | F | 160 | 2015-09-10 | | 3 | Henry | 2 | 23 | M | 185 | 2015-05-31 | | 6 | John | 2 | 21 | M | 172 | 2015-11-11 | | 8 | Susan | 4 | 23 | F | 170 | 2015-10-01 | +----+-------+---------+------+------+--------+------------+ 4 rows in set (0.04 sec)【範例 8】在表 tb_students_info 中查詢註冊日期在 2015-10-01 和 2016-05-01 之間的學生的資訊,輸入的 SQL 語句和執行結果如下所示。
mysql> SELECT * FROM tb_students_info -> WHERE login_date -> BETWEEN '2015-10-01' -> AND '2016-05-01'; +----+-------+---------+------+------+--------+------------+ | id | name | dept_id | age | sex | height | login_date | +----+-------+---------+------+------+--------+------------+ | 5 | Jim | 1 | 24 | M | 175 | 2016-01-15 | | 6 | John | 2 | 21 | M | 172 | 2015-11-11 | | 7 | Lily | 6 | 22 | F | 165 | 2016-02-26 | | 8 | Susan | 4 | 23 | F | 170 | 2015-10-01 | +----+-------+---------+------+------+--------+------------+ 4 rows in set (0.02 sec)