MySQL 的 NOT LIKE
運算符可用於搜索與特定字符串模式不匹配的值。我們可以將兩個通配符與NOT LIKE
運算符一起使用:%
(百分比)和_
(下劃線)。
通配符 |
說明 |
---|---|
|
代替零個、一個或多個字符 |
|
代替一個角色 |
用法
SELECT column_name(s)
FROM table_name
WHERE column_name NOT LIKE value;
例子
考慮下表有關一些學生的信息:
student_id |
名稱 |
名字 |
day_enrolled |
年齡 |
用戶名 |
---|---|---|---|---|---|
1 |
Sky |
Towner |
2015-12-03 |
17 |
stowner1 |
2 |
Ben |
Davis |
2016-04-20 |
19 |
bdavis2 |
3 |
Travis |
Apple |
2018-08-14 |
18 |
tapple3 |
4 |
Arthur |
David |
2016-04-01 |
16 |
adavid4 |
5 |
Benjamin |
Town |
2014-01-01 |
17 |
btown5 |
可以使用此處的代碼創建上述示例表。
百分比通配符
要返回 lname
不以 'Dav'
開頭的所有行:
SELECT * FROM students WHERE lname NOT LIKE 'Dav%';
+------------+----------+--------+--------------+------+----------+
| student_id | fname | lname | day_enrolled | age | username |
+------------+----------+--------+--------------+------+----------+
| 1 | Sky | Towner | 2015-12-03 | 17 | stowner1 |
| 3 | Travis | Apple | 2018-08-14 | 18 | tapple3 |
| 5 | Benjamin | Town | 2014-01-01 | 17 | btown5 |
+------------+----------+--------+--------------+------+----------+
請注意, %
表示 'Dav'
之後的任意數量的字符。
要返回 username
不包含 'dav'
的所有行:
SELECT * FROM students WHERE username NOT LIKE '%dav%';
+------------+----------+--------+--------------+------+----------+
| student_id | fname | lname | day_enrolled | age | username |
+------------+----------+--------+--------------+------+----------+
| 1 | Sky | Towner | 2015-12-03 | 17 | stowner1 |
| 3 | Travis | Apple | 2018-08-14 | 18 | tapple3 |
| 5 | Benjamin | Town | 2014-01-01 | 17 | btown5 |
+------------+----------+--------+--------------+------+----------+
將 %
放在開頭和結尾允許雙端搜索。
下劃線通配符
要返回名字不以 'S'
開頭並以 'y'
結尾的三個字母的學生:
SELECT * FROM students WHERE fname NOT LIKE 'S_y';
+------------+----------+-------+--------------+------+----------+
| student_id | fname | lname | day_enrolled | age | username |
+------------+----------+-------+--------------+------+----------+
| 2 | Ben | Davis | 2016-04-20 | 19 | bdavis2 |
| 3 | Travis | Apple | 2018-08-14 | 18 | tapple3 |
| 4 | Arthur | David | 2016-04-01 | 16 | adavid4 |
| 5 | Benjamin | Town | 2014-01-01 | 17 | btown5 |
+------------+----------+-------+--------------+------+----------+
此查詢返回名字不以 'S'
開頭、以 'y'
結尾且包含 3 個字符的學生。我們返回除 'Sky Towner'
之外的所有學生。
組合通配符
要返回 username
沒有 't'
作為第二個字符的學生:
SELECT * FROM students WHERE username NOT LIKE '_t%';
+------------+--------+-------+--------------+------+----------+
| student_id | fname | lname | day_enrolled | age | username |
+------------+--------+-------+--------------+------+----------+
| 2 | Ben | Davis | 2016-04-20 | 19 | bdavis2 |
| 3 | Travis | Apple | 2018-08-14 | 18 | tapple3 |
| 4 | Arthur | David | 2016-04-01 | 16 | adavid4 |
+------------+--------+-------+--------------+------+----------+
相關用法
- MySQL NOT REGEXP用法及代碼示例
- MySQL NOT IN用法及代碼示例
- MySQL NOT BETWEEN用法及代碼示例
- MySQL NOT用法及代碼示例
- MySQL NOW()用法及代碼示例
- MySQL NOW方法用法及代碼示例
- MySQL NULLIF( )用法及代碼示例
- MySQL ROUND()用法及代碼示例
- MySQL REPEAT()用法及代碼示例
- MySQL POWER()用法及代碼示例
- MySQL LEAD() and LAG()用法及代碼示例
- MySQL IS_IPV4()用法及代碼示例
- MySQL RADIANS方法用法及代碼示例
- MySQL VARIANCE方法用法及代碼示例
- MySQL WEEK()用法及代碼示例
- MySQL TIME_FORMAT方法用法及代碼示例
- MySQL CURTIME()用法及代碼示例
- MySQL weekofyear()用法及代碼示例
- MySQL Convert()用法及代碼示例
- MySQL IS NOT用法及代碼示例
- MySQL FROM_BASE64()用法及代碼示例
- MySQL LEFT方法用法及代碼示例
- MySQL UCASE方法用法及代碼示例
- MySQL PI()用法及代碼示例
- MySQL CONCAT()用法及代碼示例
注:本文由純淨天空篩選整理自Arthur Yanagisawa大神的英文原創作品 MySQL | NOT LIKE。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。