mysql 中查询语句表达式REGEXP用法
来源:互联网 发布:算法工程师主要做什么 编辑:程序博客网 时间:2024/05/29 10:08
mysql 中用REGEXP字符来匹配表达式
用法: 属性名
有如下表:
table employee
+—–+——+———–+——+——+——————–+
| num | d_id | name | age | sex | homeaddress |
+—–+——+———–+——+——+——————–+
| 1 | 1001 | 张三 | 26 | 男 | 北京海淀区 |
| 2 | 1001 | 李四 | 24 | 女 | 北京昌平区 |
| 3 | 1002 | 王五 | 25 | 男 | 湖南长沙市 |
| 4 | 1004 | Aric | 15 | 男 | England |
| 5 | 1999 | 李三思 | 17 | 男 | 海南省三亚市 |
+—–+——+———–+——+——+——————–+
1.查以特定字符或字符串开头的记录:
使用”^”
select * from employee where homeaddress REGEXP '^北京';
查询结果如下:
+—–+——+——–+——+——+—————–+
| num | d_id | name | age | sex | homeaddress |
+—–+——+——–+——+——+—————–+
| 1 | 1001 | 张三 | 26 | 男 | 北京海淀区 |
| 2 | 1001 | 李四 | 24 | 女 | 北京昌平区 |
+—–+——+——–+——+——+—————–+
2.查询以特定字符或字符串结尾的记录:
用法:’字符或字符串$’
select * from employee where homeaddress regexp '市$';
结果如下:
+—–+——+———–+——+——+——————–+
| num | d_id | name | age | sex | homeaddress |
+—–+——+———–+——+——+——————–+
| 3 | 1002 | 王五 | 25 | 男 | 湖南长沙市 |
| 5 | 1999 | 李三思 | 17 | 男 | 海南省三亚市 |
+—–+——+———–+——+——+——————–+
3.用‘.’可以代替字符串中的任意字符:
用法如下:
select * from employee where name regexp '.五';
select * from employee where name regexp '李..思';#经过摸索,我发现在中间的汉,需要用两个.代替。#3.1.插入提条记录如下:insert into employee values(null,1898,'lily','女','HaiNanSanya');#3.2.查询lilyselect * from employee where name regexp 'l.ly';
3.1.结果如下:
+—–+——+——–+——+——+—————–+
| num | d_id | name | age | sex | homeaddress |
+—–+——+——–+——+——+—————–+
| 3 | 1002 | 王五 | 25 | 男 | 湖南长沙市 |
+—–+——+——–+——+——+—————–+
1 row in set (0.00 sec)
3.2.
+—–+——+——+——+——+————-+
| num | d_id | name | age | sex | homeaddress |
+—–+——+——+——+——+————-+
| 6 | 1898 | lily | 18 | 女 | HaiNanSanya |
+—–+——+——+——+——+————-+
1 row in set (0.00 sec)
4.匹配指定字符串中任意一个
用法:[ ]
select * from employee where name regexp '[五l三]';
结果:+—–+——+———–+——+——+——————–+
| num | d_id | name | age | sex | homeaddress |
+—–+——+———–+——+——+——————–+
| 1 | 1001 | 张三 | 26 | 男 | 北京海淀区 |
| 3 | 1002 | 王五 | 25 | 男 | 湖南长沙市 |
| 5 | 1999 | 李三思 | 17 | 男 | 海南省三亚市 |
| 6 | 1898 | lily | 18 | 女 | HaiNanSanya |
+—–+——+———–+——+——+——————–+
4 rows in set (0.00 sec)
5.搜索制定区间
用法:[]
select * from employee where num regexp '[1-5]';#查找num在1-5之间的记录
结果如下:
+—–+——+———–+——+——+——————–+
| num | d_id | name | age | sex | homeaddress |
+—–+——+———–+——+——+——————–+
| 1 | 1001 | 张三 | 26 | 男 | 北京海淀区 |
| 2 | 1001 | 李四 | 24 | 女 | 北京昌平区 |
| 3 | 1002 | 王五 | 25 | 男 | 湖南长沙市 |
| 4 | 1004 | Aric | 15 | 男 | England |
| 5 | 1999 | 李三思 | 17 | 男 | 海南省三亚市 |
+—–+——+———–+——+——+——————–+
5 rows in set (0.01 sec)
6.搜索制定区间之外的记录:
用法:[^1-5]
select * from employee where num regexp '[^1-5]';
结果如下:
+—–+——+——+——+——+————-+
| num | d_id | name | age | sex | homeaddress |
+—–+——+——+——+——+————-+
| 6 | 1898 | lily | 18 | 女 | HaiNanSanya |
+—–+——+——+——+——+————-+
7.查询指定字符串
#查询homeaddress在三亚市的记录select * from employee where homeaddress regexp '三亚市';
结果如下:
+—–+——+———–+——+——+——————–+
| num | d_id | name | age | sex | homeaddress |
+—–+——+———–+——+——+——————–+
| 5 | 1999 | 李三思 | 17 | 男 | 海南省三亚市 |
+—–+——+———–+——+——+——————–+
1 row in set (0.01 sec)
#查询三亚、长沙、昌平中的任意一个select * from employee where homeadress regexp '三亚|长沙|昌平';
结果如下:
+—–+——+———–+——+——+——————–+
| num | d_id | name | age | sex | homeaddress |
+—–+——+———–+——+——+——————–+
| 2 | 1001 | 李四 | 24 | 女 | 北京昌平区 |
| 3 | 1002 | 王五 | 25 | 男 | 湖南长沙市 |
| 5 | 1999 | 李三思 | 17 | 男 | 海南省三亚市 |
+—–+——+———–+——+——+——————–+
3 rows in set (0.00 sec)
8.用×和+来匹配多个字符:
select * from employee where name regexp 'l*y';#表示查寻name 下字母y前面有0个或多个l
+—–+——+——+——+——+————-+
| num | d_id | name | age | sex | homeaddress |
+—–+——+——+——+——+————-+
| 6 | 1898 | lily | 18 | 女 | HaiNanSanya |
+—–+——+——+——+——+————-+
1 row in set (0.01 sec)
select * from employee where name regexp 'l+y';#表示查询字母y前面至少有一个l
结果:
+—–+——+——+——+——+————-+
| num | d_id | name | age | sex | homeaddress |
+—–+——+——+——+——+————-+
| 6 | 1898 | lily | 18 | 女 | HaiNanSanya |
+—–+——+——+——+——+————-+
1 row in set (0.01 sec)
select * from employee where name regexp 'a+y';#表示查询字母y前面至少有一个a
结果:
mysql> select * from employee where name regexp ‘a+y’;
Empty set (0.00 sec)
9.按照指定字符出现的次数 查询
#先插一条记录insert into employee values (null,1880,'aaaccsd',28,'女','America');#查询如下:select * from employee where name regexp 'a{3}';
结果:
+—–+——+———+——+——+————-+
| num | d_id | name | age | sex | homeaddress |
+—–+——+———+——+——+————-+
| 7 | 1880 | aaaccsd | 28 | 女 | America |
+—–+——+———+——+——+————-+
- mysql 中查询语句表达式REGEXP用法
- mysql中replace、regexp正则表达式替换用法
- MySQL中REGEXP正则表达式
- mysql中REGEXP正则表达式使用
- MySQL中REGEXP正则表达式使用大全
- MySql 查询中的 REGEXP
- ASP中RegExp对象正则表达式功能用法
- SQL中LIKE模糊查询与REGEXP用法说明
- Mysql中使用正则表达式搜索——关键字REGEXP(., |, [], ^, $, \\)
- mysql中like及regexp查询反斜杠
- MySQL正则表达式 REGEXP详解
- Mysql使用正则表达式-regexp
- MySQL正则表达式-使用REGEXP
- mysql模糊查询like/REGEXP
- [mysql] replace、regexp的用法
- MYSQL GROUP_CONCAT 与REGEXP用法
- FLex中RegExp正则表达式
- golang 中regexp包用法
- QT实现应用程序重启
- UIImageView 实现一组动画
- 排列组合算法实现
- 基本图算法之图的表示
- AsyncTask入门
- mysql 中查询语句表达式REGEXP用法
- 通过Wifi调试运行Android应用的IntelliJ/AndroidStudio插件:AndroidWiFiADB
- 期末总结
- SPIR-V 研究:编译器基本原理(三) - Chomsky文法分类
- 循环单向链表
- 浅读原型
- centos rm -rf 恢复删除的文件
- 【Codeforces 597C】 Subsequences - DP 主席树
- 项目第二天