当前位置: 移动技术网 > IT编程>数据库>Mysql > mysql 正则表达式查询含有非数字和字符的记录

mysql 正则表达式查询含有非数字和字符的记录

2017年12月12日  | 移动技术网IT编程  | 我要评论

比如我们有一张school表,里面有一个字段county_name,现在我们要查询county_name字段中包含a-w字母和数字以外字符的记录,那么sql该如何写呢?请看下面的写法:

select * from info where name regexp '[^a-w0-9]'; 

mysql中正则表达式使用regexp关键字,[^a-w0-9]表示匹配除了a-w字母和数字以外的字符。

下面向大家介绍mysql正则表达式的其他使用实例:

匹配名称含有1000的所有行

select * from a1 where name regexp '1000'

匹配以000结尾的所有行,(.正则中表示:匹配任意一个字符)

select * from a1 where name regexp '.000'

mysql正则大小写都会匹配,为区分大小写可使用binary关键字,如:

select * from a1 where name like binary '%j%'  #使用like+通配符匹配大写j
select * from a1 where name regexp binary 'j'  #使用正则匹配小写j

|为正则表达式的or操作符,表示匹配其中之一

select * from a1 where name regexp binary 'a|j|g' 

匹配特定字符,使用[]括起来的字符,将会匹配其中任意单个字符。

select * from a1 where name regexp '[12]st'

以上'[12]st'正则表达式,[12]定义一组字符,它的意思是匹配1或2

^ 匹配字符开始的部分

select * from info where name regexp '^l'; //从info表name字段中查询以l开头的记录 

$ 匹配字符结束的部分

select * from info where name regexp 'c$'; //从info表name字段中查询以c结尾的记录 

. 匹配字符串中的任意一个字符,包括回车和换行

select * from info where name regexp '^l..y$'; //从info表name字段中查询以l开头y结尾中间有两个任意字符的记录 

[字符集合]匹配字符集合中的任意字符

select * from info where name regexp '[ceo]'; //从info表name字段中查询包含c、e、o三个字母中任意一个的记录 

[^字符集合]匹配除了字符集合外的任意字符

select * from info where name regexp '[^a-w0-9]'; //从info表name字段中查询包含a-w字母和数字以外字符的记录 

s1|s2|s3 匹配s1s2s3中的任意一个

select * from info where name regexp 'ic'; //从info表name字段中查询包含'ic'的记录 

* 代表多个该字符前的字符,包括0个或1个

select * from info where name regexp 'a*c'; 从info表name字段中查询c之前出现过a的记录 

+ 代表多个该字符前的字符,包括1个

select * from info where name regexp 'a+c';//从info表name字段中查询c之前出现过a的记录 

字符串{n} 字符串出现n次

select * from info where name regexp 'a{3}'; //从info表name字段中查询出现过a3次的记录

字符串{m,n}字符串最少出现m次,最多出现n次

select * from info where name regexp 'ab{1,3}'; //从info表name字段中查询ab出现最少1次最多3次的记录

mysql中自带通配符(like关键词),%可以表示任意长度的字符(包括0), -可以表示单个字符

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网