mysql中如何使用正則表達(dá)式查詢
更新時(shí)間:2014年06月11日 09:18:52 作者:
這篇文章主要介紹了mysql中如何使用正則表達(dá)式查詢,需要的朋友可以參考下
基本形式
屬性名 regexp ‘匹配方式'
正則表達(dá)式的模式字符
^ 匹配字符開始的部分
eg1: 從info表name字段中查詢以L開頭的記錄
select * from info where name regexp '^L';
eg2: 從info表name字段中查詢以aaa開頭的記錄
select * from info where name regexp '^aaa';
$ 匹配字符結(jié)束的部分
eg1: 從info表name字段中查詢以c結(jié)尾的記錄
select * from info where name regexp 'c$';
eg2: 從info表name字段中查詢以aaa結(jié)尾的記錄
select * from info where name regexp 'aaa$';
. 匹配字符串中的任意一個(gè)字符,包括回車和換行
eg1: 從info表name字段中查詢以L開頭y結(jié)尾中間有兩個(gè)任意字符的記錄
select * from info where name regexp '^L..y$';
[字符集合]匹配字符集合中的任意字符
eg1: 從info表name字段中查詢包含c、e、o三個(gè)字母中任意一個(gè)的記錄
select * from info where name regexp '[ceo]';
eg2: 從info表name字段中查詢包含數(shù)字的記錄
select * from info where name regexp '[0-9]';
eg3: 從info表name字段中查詢包含數(shù)字或a、b、c三個(gè)字母中任意一個(gè)的記錄
select * from info where name regexp '[0-9a-c]';
[^字符集合]匹配除了字符集合外的任意字符
eg1: 從info表name字段中查詢包含a-w字母和數(shù)字以外字符的記錄
select * from info where name regexp '[^a-w0-9]';
s1|s2|s3 匹配s1s2s3中的任意一個(gè)
eg1: 從info表name字段中查詢包含'ic'的記錄
select * from info where name regexp 'ic';
eg2: 從info表name字段中查詢包含ic、uc、ab三個(gè)字符串中任意一個(gè)的記錄
select * from info where name regexp 'ic|uc|ab';
* 代表多個(gè)該字符前的字符,包括0個(gè)或1個(gè)
eg1: 從info表name字段中查詢c之前出現(xiàn)過a的記錄
select * from info where name regexp 'a*c';
+ 代表多個(gè)該字符前的字符,包括1個(gè)
eg1: 從info表name字段中查詢c之前出現(xiàn)過a的記錄
select * from info where name regexp 'a+c';(注意比較結(jié)果!)
字符串{N} 字符串出現(xiàn)N次
eg1: 從info表name字段中查詢出現(xiàn)過a3次的記錄
select * from info where name regexp 'a{3}';
字符串{M,N}字符串最少出現(xiàn)M次,最多出現(xiàn)N次
eg1: 從info表name字段中查詢ab出現(xiàn)最少1次最多3次的記錄
select * from info where name regexp 'ab{1,3}';
MYSQL中自帶通配符(LIKE關(guān)鍵詞)
%可以表示任意長度的字符(包括0)
-可以表示單個(gè)字符
屬性名 regexp ‘匹配方式'
正則表達(dá)式的模式字符
^ 匹配字符開始的部分
eg1: 從info表name字段中查詢以L開頭的記錄
select * from info where name regexp '^L';
eg2: 從info表name字段中查詢以aaa開頭的記錄
select * from info where name regexp '^aaa';
$ 匹配字符結(jié)束的部分
eg1: 從info表name字段中查詢以c結(jié)尾的記錄
select * from info where name regexp 'c$';
eg2: 從info表name字段中查詢以aaa結(jié)尾的記錄
select * from info where name regexp 'aaa$';
. 匹配字符串中的任意一個(gè)字符,包括回車和換行
eg1: 從info表name字段中查詢以L開頭y結(jié)尾中間有兩個(gè)任意字符的記錄
select * from info where name regexp '^L..y$';
[字符集合]匹配字符集合中的任意字符
eg1: 從info表name字段中查詢包含c、e、o三個(gè)字母中任意一個(gè)的記錄
select * from info where name regexp '[ceo]';
eg2: 從info表name字段中查詢包含數(shù)字的記錄
select * from info where name regexp '[0-9]';
eg3: 從info表name字段中查詢包含數(shù)字或a、b、c三個(gè)字母中任意一個(gè)的記錄
select * from info where name regexp '[0-9a-c]';
[^字符集合]匹配除了字符集合外的任意字符
eg1: 從info表name字段中查詢包含a-w字母和數(shù)字以外字符的記錄
select * from info where name regexp '[^a-w0-9]';
s1|s2|s3 匹配s1s2s3中的任意一個(gè)
eg1: 從info表name字段中查詢包含'ic'的記錄
select * from info where name regexp 'ic';
eg2: 從info表name字段中查詢包含ic、uc、ab三個(gè)字符串中任意一個(gè)的記錄
select * from info where name regexp 'ic|uc|ab';
* 代表多個(gè)該字符前的字符,包括0個(gè)或1個(gè)
eg1: 從info表name字段中查詢c之前出現(xiàn)過a的記錄
select * from info where name regexp 'a*c';
+ 代表多個(gè)該字符前的字符,包括1個(gè)
eg1: 從info表name字段中查詢c之前出現(xiàn)過a的記錄
select * from info where name regexp 'a+c';(注意比較結(jié)果!)
字符串{N} 字符串出現(xiàn)N次
eg1: 從info表name字段中查詢出現(xiàn)過a3次的記錄
select * from info where name regexp 'a{3}';
字符串{M,N}字符串最少出現(xiàn)M次,最多出現(xiàn)N次
eg1: 從info表name字段中查詢ab出現(xiàn)最少1次最多3次的記錄
select * from info where name regexp 'ab{1,3}';
MYSQL中自帶通配符(LIKE關(guān)鍵詞)
%可以表示任意長度的字符(包括0)
-可以表示單個(gè)字符
相關(guān)文章
淺談SQLite時(shí)間函數(shù)的使用說明與總結(jié)分析
本篇文章是對SQLite時(shí)間函數(shù)的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05MySQL timestamp自動(dòng)更新時(shí)間分享
在mysql中timestamp數(shù)據(jù)類型是一個(gè)比較特殊的數(shù)據(jù)類型,他可以自動(dòng)在你不使用程序更新情況下只要你更新了記錄timestamp會(huì)自動(dòng)更新時(shí)間2013-06-06Mysql將一個(gè)表中的某一列數(shù)據(jù)復(fù)制到另一個(gè)表中某一列里的方法
今天小編就為大家分享一篇關(guān)于Mysql將一個(gè)表中的某一列數(shù)據(jù)復(fù)制到另一個(gè)表中某一列里的方法,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-03-03