您好,登錄后才能下訂單哦!
這篇文章主要介紹“MySQL REGEXP怎么使用”,在日常操作中,相信很多人在MySQL REGEXP怎么使用問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”MySQL REGEXP怎么使用”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
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怎么使用”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。