共計 4591 個字符,預計需要花費 12 分鐘才能閱讀完成。
這篇文章主要介紹“MySQL REGEXP 怎么使用”,在日常操作中,相信很多人在 MySQL REGEXP 怎么使用問題上存在疑惑,丸趣 TV 小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”MySQL REGEXP 怎么使用”的疑惑有所幫助!接下來,請跟著丸趣 TV 小編一起來學習吧!
1. 關于 NULL
普通的比較運算符用于 NULL,返回的結果都是 NULL。
mysql select 0 = null, 1 null, 2 null, 3 null, 4 = null, 5 = null;
+———-+———–+———-+———-+———–+———–+
| 0 = null | 1 null | 2 null | 3 null | 4 = null | 5 = null |
+———-+———–+———-+———-+———–+———–+
| NULL | NULL | NULL | NULL | NULL | NULL |
+———-+———–+———-+———-+———–+———–+
1 row in set (0.00 sec)
[@more@] 要判斷一個值是否為 NULL,應該使用 IS NULL、IS NOT NULL 或 =(NULL 安全地等于)等運算符。
mysql select 0 is null, null is null;
+———–+————–+
| 0 is null | null is null |
+———–+————–+
| 0 | 1 |
+———–+————–+
1 row in set (0.00 sec)
mysql select 0 is not null, null is not null;
+—————+——————+
| 0 is not null | null is not null |
+—————+——————+
| 1 | 0 |
+—————+——————+
1 row in set (0.00 sec)
mysql select 0 = null, null = null;
+————+—————+
| 0 = null | null = null |
+————+—————+
| 0 | 1 |
+————+—————+
1 row in set (0.00 sec)
在 MySQL 中,NULL 不同于空值。
mysql select IS NULL;
+————+
| IS NULL |
+————+
| 0 |
+————+
1 row in set (0.00 sec)
2.REGEXP
REGEXP 運算符可以執行較復雜的字符串比較運算,這主要通過正則表達式來實現。正則表達式由標準字符和專門定義匹配模式的元字符混合組成,下表列出了正則表達式中經常使用的元字符:
元字符 作用
+ 匹配 1 個或更多個前面字符的值
* 匹配 0 個或更多個前面字符的值
? 匹配 0 個或 1 前面字符的值
. 匹配任意字符
^ 匹配字符串的開始部分
$ 匹配字符串的末尾部分
s 匹配單個空白空間字符,包括制表符合換行符
S 匹配空白空間字符以外的一切字符
d 匹配 0 到 9 之間的數字
w 匹配字母、數字和下滑線字符
W 匹配用 w 不能匹配的任意字符
mysql select google regexp go+ogle , google regexp go*ogle , google reg
exp go?ogle
+—————————+—————————+———————–
—-+
| google regexp go+ogle | google regexp go*ogle | google regexp go?og
le |
+—————————+—————————+———————–
—-+
| 1 | 1 |
1 |
+—————————+—————————+———————–
—-+
1 row in set (0.00 sec)
mysql select google regexp go+gle , google regexp go*gle , google regex
p go?gle
+————————–+————————–+————————-
-+
| google regexp go+gle | google regexp go*gle | google regexp go?gle
|
+————————–+————————–+————————-
-+
| 1 | 1 | 0
|
+————————–+————————–+————————-
-+
1 row in set (0.00 sec)
mysql select google regexp gooo+gle , google regexp gooo*gle , google r
egexp gooo?gle
+—————————-+—————————-+———————
——-+
| google regexp gooo+gle | google regexp gooo*gle | google regexp goo
o?gle |
+—————————-+—————————-+———————
——-+
| 0 | 1 |
1 |
+—————————-+—————————-+———————
——-+
1 row in set (0.00 sec)
mysql select google regexp ^goo , google regexp goo$
+————————+————————+
| google regexp ^goo | google regexp goo$ |
+————————+————————+
| 1 | 0 |
+————————+————————+
1 row in set (0.00 sec)
mysql select google regexp ^gle , google regexp gle$
+————————+————————+
| google regexp ^gle | google regexp gle$ |
+————————+————————+
| 0 | 1 |
+————————+————————+
1 row in set (0.00 sec)
mysql select fifi regexp ^fi , fifi regexp fi$ , fifi regexp ^fi$ , f
ifi regexp ^fifi$
+———————+———————+———————-+————
————+
| fifi regexp ^fi | fifi regexp fi$ | fifi regexp ^fi$ | fifi rege
xp ^fifi$ |
+———————+———————+———————-+————
————+
| 1 | 1 | 0 |
1 |
+———————+———————+———————-+————
————+
1 row in set (0.00 sec)
3. 系統信息函數
下面列舉一些常用的系統信息函數:
user() 或 system_user() 返回當前登陸用戶名
connection_id() 返回當前用戶的連接 ID
database() 返回當前數據庫名
version() 返回 MySQL 服務器的版本
mysql select user(), connection_id(), database(), version();
+—————-+—————–+————+——————+
| user() | connection_id() | database() | version() |
+—————-+—————–+————+——————+
| root@localhost | 2 | ggyy | 5.1.34-community |
+—————-+—————–+————+——————+
1 row in set (0.40 sec)
undefinedundefinedundefinedundefinedundefined
到此,關于“MySQL REGEXP 怎么使用”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注丸趣 TV 網站,丸趣 TV 小編會繼續努力為大家帶來更多實用的文章!