mysql 命令行操作 (五)正则表达式

除了使用like语句"%"以外,myql支持正则表达式匹配,使用regexp语句,测试数据如下:

mysql> select * from website;
+----+---------------+---------------------------+-------+---------+
| id | name          | url                       | alexa | country |
+----+---------------+---------------------------+-------+---------+
|  1 | Google        | https://www.google.com/   |     1 | USA     |
|  2 | 淘宝          | https:/www.taobao.com/    |    13 | CN      |
|  3 | 菜鸟教程      | http://www.runoob.com     |  4689 | CN      |
|  4 | 微博          | http://weibo.com/         |    20 | CN      |
|  5 | Facebook      | https://www.facebook.com/ |     3 | USA     |
|  6 | stackoverflow | http://stackoverflow.com/ |     0 | IND     |
+----+---------------+---------------------------+-------+---------+
6 rows in set (0.00 sec)

1.查找url中开头为https的记录:

mysql> select * from website where url regexp '^https';
+----+----------+---------------------------+-------+---------+
| id | name     | url                       | alexa | country |
+----+----------+---------------------------+-------+---------+
|  1 | Google   | https://www.google.com/   |     1 | USA     |
|  2 | 淘宝     | https:/www.taobao.com/    |    13 | CN      |
|  5 | Facebook | https://www.facebook.com/ |     3 | USA     |
+----+----------+---------------------------+-------+---------+
3 rows in set (0.00 sec)

2.查找url中以com结尾的所有数据:

mysql> select * from website where url regexp 'com$';
+----+--------------+-----------------------+-------+---------+
| id | name         | url                   | alexa | country |
+----+--------------+-----------------------+-------+---------+
|  3 | 菜鸟教程     | http://www.runoob.com |  4689 | CN      |
+----+--------------+-----------------------+-------+---------+
1 row in set (0.00 sec)

3.查找url中包含www的所有数据:

mysql> select * from website where url regexp 'www';
+----+--------------+---------------------------+-------+---------+
| id | name         | url                       | alexa | country |
+----+--------------+---------------------------+-------+---------+
|  1 | Google       | https://www.google.com/   |     1 | USA     |
|  2 | 淘宝         | https:/www.taobao.com/    |    13 | CN      |
|  3 | 菜鸟教程     | http://www.runoob.com     |  4689 | CN      |
|  5 | Facebook     | https://www.facebook.com/ |     3 | USA     |
+----+--------------+---------------------------+-------+---------+

4.查找url中以abcd四个字母开头的或者以com结尾的记录:

mysql> select * from website where url regexp '^[abcd]|com$';
+----+--------------+-----------------------+-------+---------+
| id | name         | url                   | alexa | country |
+----+--------------+-----------------------+-------+---------+
|  3 | 菜鸟教程     | http://www.runoob.com |  4689 | CN      |
+----+--------------+-----------------------+-------+---------+
1 row in set (0.00 sec)

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值