您的位置:首页 > 数据库 > MySQL

mysql 中查询语句表达式REGEXP用法

2015-12-21 21:14 871 查看
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.查询lily
select * 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 |

+—–+——+———+——+——+————-+
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: