您好,登錄后才能下訂單哦!
這篇文章主要介紹“MySQL REGEXP怎么使用”,在日常操作中,相信很多人在MySQL REGEXP怎么使用問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”MySQL REGEXP怎么使用”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!
1.關(guān)于NULL
普通的比較運(yùn)算符用于NULL,返回的結(jié)果都是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,應(yīng)該使用IS NULL、IS NOT NULL或<=>(NULL安全地等于)等運(yùn)算符。
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運(yùn)算符可以執(zhí)行較復(fù)雜的字符串比較運(yùn)算,這主要通過正則表達(dá)式來實(shí)現(xiàn)。正則表達(dá)式由標(biāo)準(zhǔn)字符和專門定義匹配模式的元字符混合組成,下表列出了正則表達(dá)式中經(jīng)常使用的元字符:
元字符 作用
+ 匹配1個或更多個前面字符的值
* 匹配0個或更多個前面字符的值
? 匹配0個或1前面字符的值
. 匹配任意字符
^ 匹配字符串的開始部分
$ 匹配字符串的末尾部分
s 匹配單個空白空間字符,包括制表符合換行符
S 匹配空白空間字符以外的一切字符
d 匹配0到9之間的數(shù)字
w 匹配字母、數(shù)字和下滑線字符
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.系統(tǒng)信息函數(shù)
下面列舉一些常用的系統(tǒng)信息函數(shù):
user()或system_user() 返回當(dāng)前登陸用戶名
connection_id() 返回當(dāng)前用戶的連接ID
database() 返回當(dāng)前數(shù)據(jù)庫名
version() 返回MySQL服務(wù)器的版本
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
到此,關(guān)于“MySQL REGEXP怎么使用”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。