這裡並沒有前情回顧 NULL值操作 Null是一個特殊的值,它表示著“沒有值”或者“未知值”,記住它是一個與眾不同的值。 為了測試NULL,不能用算數比較操作符 不信你試試 操作如下: SELECT 1 = NULL, 1 <> NULL, 1 < NULL, 1 > NULL; mysql> SE ...
這裡並沒有前情回顧
NULL值操作
Null是一個特殊的值,它表示著“沒有值”或者“未知值”,記住它是一個與眾不同的值。
為了測試NULL,不能用算數比較操作符 不信你試試
操作如下:
SELECT 1 = NULL, 1 <> NULL, 1 < NULL, 1 > NULL;
mysql> SELECT 1 = NULL, 1 <> NULL, 1 < NULL, 1 > NULL;
+----------+-----------+----------+----------+
| 1 = NULL | 1 <> NULL | 1 < NULL | 1 > NULL |
+----------+-----------+----------+----------+
| NULL | NULL | NULL | NULL |
+----------+-----------+----------+----------+
1 row in set (0.00 sec)
mysql>
都說了不能了,你還試。
那應該用什麼辦法試試呢?(搞事情)
遠古時代的上神在升天之前曾經遺留下這兩個操作符在人間供凡人們使用,他們分別是:
IS NULL 和 IS NOT NULL 這兩個操作符。
操作如下:
SELECT 1 IS NULL, 1 IS NOT NULL;
mysql> SELECT 1 IS NULL, 1 IS NOT NULL;
+-----------+---------------+
| 1 IS NULL | 1 IS NOT NULL |
+-----------+---------------+
| 0 | 1 |
+-----------+---------------+
1 row in set (0.08 sec)
mysql>
溫馨提示(書上又瞎操心了):在GROUP BY 中兩個NULL值視為相同(我還沒講GROUP BY呢)。
執行ORDER BY時, 如果運行 ORDER BY ... ASC (升序), 則NULL值出現在最前面,若運行ORDER BY ...DESC ,則NULL值出現在最後面。
mysql> SELECT 0 IS NULL, 0 IS NOT NULL, '' IS NULL, '' IS NOT NULL;
+-----------+---------------+------------+----------------+
| 0 IS NULL | 0 IS NOT NULL | '' IS NULL | '' IS NOT NULL |
+-----------+---------------+------------+----------------+
| 0 | 1 | 0 | 1 |
+-----------+---------------+------------+----------------+
1 row in set (0.00 sec)
mysql>
模式匹配
註意:使用SQL模式匹配的時候,不能使用= 和 != 而應該使用 LIKE 或 NOT LIKE 比較操作符。還有忽略大小寫。
SQL模式匹配允許你使用 "_" 匹配任何單個字元,而 "%" 匹配任意數目字元(包括零字元)。
舉個慄子:我想找以'b'開頭的名字。
操作如下:
SELECT * FROM pet WHERE name LIKE 'b%';
mysql> SELECT * FROM pet
-> WHERE name LIKE 'b%';
+--------+--------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+--------+---------+------+------------+------------+
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
| Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 |
+--------+--------+---------+------+------------+------------+
2 rows in set (0.11 sec)
mysql>
再舉個慄子:我想找出以'fy'結尾的名字。
操作如下:
SELECT * FROM pet WHERE name LIKE '%fy';
mysql> SELECT * FROM pet
-> WHERE name LIKE '%fy';
+--------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+--------+--------+---------+------+------------+-------+
| Fluffy | Harold | cat | f | 1993-02-04 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
+--------+--------+---------+------+------------+-------+
2 rows in set (0.00 sec)
mysql>
接著舉個慄子:我想找出名字中包含'w'的。(動態查找(學長告訴我的))
操作如下:
SELECT * FROM pet WHERE name LIKE '%w%';
mysql> SELECT * FROM pet
-> WHERE name LIKE '%w%';
+----------+-------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+----------+-------+---------+------+------------+------------+
| Whistler | Gwen | bird | NULL | 1997-12-09 | NULL |
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 |
+----------+-------+---------+------+------------+------------+
3 rows in set (0.00 sec)
mysql>
還是慄子:某個腦殘客戶要找五個字的名字。這時候機智的你(•́⌄•́๑)૭✧就應該用'_'來匹配了
操作如下:
SELECT * FROM pet WHERE name LIKE '_____';(別數了是五個下劃線)
mysql> SELECT * FROM pet
-> WHERE name LIKE '_____';
+-------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+-------+--------+---------+------+------------+-------+
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
+-------+--------+---------+------+------------+-------+
2 rows in set (0.00 sec)
mysql>
如果是非常複雜的匹配原則(比如我要在text 中找有沒有身份證號)就會使用到正則表達式了。
這是就用到了這兩個操作符 REGEXP 和 NOT REGEXP(或RLIKE 和 NOT RLIKE,他們是同義詞)。
先列舉幾個簡單的,詳細的之後會有的。如果要是不更新的話。。。
- '.' 匹配任何單個的字元。
- 字元類“[...]”匹配在方括弧內的任何字元。例如,“[abc]”匹配“a”、“b”或“c”。為了命名字元的範圍,使用一個“-”。“[a-z]”匹配任何字母,而“[0-9]”匹配任何數字。
- "*" 匹配零個或多個在它前面的字元。例如,“x*”匹配任何數量的“x”字元,“[0-9]*”匹配任何數量的數字,而 ".*" 匹配任何數量的任何字元。
- 為了定位一個模式以便它必須匹配被測試值的開始或結尾,在模式開始處使用“^”或在模式的結尾用“$”;
我再把上面的慄子重新敲一遍。
找出以'b'開頭的名字
操作如下:
SELECT * FROM pet WHERE name RLIKE '^b';
mysql> SELECT * FROM pet
-> WHERE name RLIKE '^b';
+--------+--------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+--------+--------+---------+------+------------+------------+
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
| Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 |
+--------+--------+---------+------+------------+------------+
2 rows in set (0.05 sec)
mysql>
如果你非要區分大小寫就應該這麼寫
SELECT * FROM pet WHERE name RLIKE BINARY '^b';
找出以'fy'結尾的名字
操作如下:
SELECT * FROM pet WHERE name RLIKE 'fy$';
mysql> SELECT * FROM pet WHERE name RLIKE 'fy$';
+--------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+--------+--------+---------+------+------------+-------+
| Fluffy | Harold | cat | f | 1993-02-04 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
+--------+--------+---------+------+------------+-------+
2 rows in set (0.00 sec)
mysql>
找出名字中有'w'的
操作如下:
SELECT * FROM pet WHERE name RLIKE 'w';
mysql> SELECT *
-> FROM pet
-> WHERE name RLIKE 'w';
+----------+-------+---------+------+------------+------------+
| name | owner | species | sex | birth | death |
+----------+-------+---------+------+------------+------------+
| Whistler | Gwen | bird | NULL | 1997-12-09 | NULL |
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 |
+----------+-------+---------+------+------------+------------+
3 rows in set (0.00 sec)
mysql>
找出5個字母的名字的pet
操作如下:
SELECT * FROM pet WHERE name RLIKE '^.....$';
mysql> SELECT * FROM pet WHERE name RLIKE '^.....$';
+-------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+-------+--------+---------+------+------------+-------+
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
+-------+--------+---------+------+------------+-------+
2 rows in set (0.00 sec)
mysql>
也可以這麼寫:
SELECT * FROM pet WHERE name RLIKE '^.{5}$';
mysql> SELECT * FROM pet WHERE name RLIKE '^.{5}$';
+-------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+-------+--------+---------+------+------------+-------+
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
+-------+--------+---------+------+------------+-------+
2 rows in set (0.00 sec)
mysql>
to be continued...