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
1.作者:Syw 2.出处:http://www.cnblogs.com/syw20170419/ 3.本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。 4.如果文中有什么错误,欢迎指出。以免更多的人被误导。 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?