MYSQL———正则表达式查询!

在使用select查询的过程中,有时会用到正则表达式对结果进行查询,将学习到的内容进行总结!

一 语法结构如下:

  

二 常用匹配方式进行示例说明

   首先创建表student,表的结构如下:

   

 1·^:查询student表中sname列已‘王’开始的姓名

select sname from student where sname regexp '^王';    #查出结果:王丽.王芳

 

 2·$:查询student表中sname列已‘文’结束的姓名 

select sname from student where sname regexp '文$';  #查出结果:尚文

 

 3. · :查询student表中sname的开始字符是‘李’,后面不做限制的姓名

select sname from student where sname regexp '李.'; #查出结果:李军

 

 4· [字符集合]:查询student表中sbirthday列,年份中包含8或者5的年份

select sbirthday from student where sbirthday regexp '[85]';  #查出结果1798,1795

 

 5.[^字符集合]:查询student表中sbirthday列,年份除了1976的其余年份

select sbirthday from student where sbirthday regexp '[^1976]';  #查出结果:1978,1974,1975

 

 6. s1|s2|s3:查询student表中sbirthday列,包含76|75|78的年份

select sbirthday from student where sbirthday regexp '76|75|78';   #查出结果:1976.1978.1975.1976

 

 7·×:查询student表中sname 列,‘文’出现的sname

select sname from student where sname regexp '文*';   #查出结果:尚文,说明:注意使用×是指查询的字符串可以有,也可以出现1次或者多次

 

 8.+:查询student表中sname 列,‘文’出现的sname

select sname from student where sname regexp '文+';  #查出结果:尚文,说明:注意使用+是指查询的字符串至少要出现1次

 

 9.字符{n}:查询student表中sname 列,‘文’出现1次的sname

 select sname from student where sname regexp '文{1}'; #查出结果:尚文,查询出了文出现1次的姓名,将1改为2,将返回NULL,因为student表中sname列没有文出现过2次的姓名

 

 10. 字符{m,n}:查询student表中sbirthday 列,‘7’出现至少2次,最多不超过3的年份

select sbirthday from student where sbirthday regexp '7{2,3}';  #查出结果:1977,1977
原文地址:https://www.cnblogs.com/syw20170419/p/6912309.html