除了使用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)