mysql 正则表达式

mysql> select * from test;
+----+----------+-------+-----------+
| id | name     | score | subject   |
+----+----------+-------+-----------+
|  1 | xiaoming |    89 | shuxue    |
|  2 | xiaohong |    89 | shuxue    |
|  3 | xiaohong |    80 | english   |
|  4 | xiaohong |    80 | physics   |
|  5 | xiaohong |    80 | astronaut |
|  6 | xiaoming |    80 | physics   |
|  7 | xiaoming |    80 | astronaut |
|  8 | xiaoming |    80 | english   |
|  9 | xiaobai  |    80 | astronaut |
| 10 | xiaobai  |    80 | english   |
| 11 | xiaobai  |    80 | physics   |
| 12 | xiaobai  |    80 | shuxue    |
| 13 | xiaohei  |    80 | astronaut |
| 14 | xiaohei  |    80 | shuxue    |
| 15 | xiaohei  |    80 | physics   |
| 16 | xiaohei  |    80 | english   |
+----+----------+-------+-----------+
16 rows in set (0.00 sec)



mysql> select * from test where subject regexp "sh";
+----+----------+-------+---------+
| id | name     | score | subject |
+----+----------+-------+---------+
|  1 | xiaoming |    89 | shuxue  |
|  2 | xiaohong |    89 | shuxue  |
|  3 | xiaohong |    80 | english |
|  8 | xiaoming |    80 | english |
| 10 | xiaobai  |    80 | english |
| 12 | xiaobai  |    80 | shuxue  |
| 14 | xiaohei  |    80 | shuxue  |
| 16 | xiaohei  |    80 | english |
+----+----------+-------+---------+
8 rows in set (0.00 sec)



mysql> select * from test where subject regexp "^sh";
+----+----------+-------+---------+
| id | name     | score | subject |
+----+----------+-------+---------+
|  1 | xiaoming |    89 | shuxue  |
|  2 | xiaohong |    89 | shuxue  |
| 12 | xiaobai  |    80 | shuxue  |
| 14 | xiaohei  |    80 | shuxue  |
+----+----------+-------+---------+
4 rows in set (0.00 sec)


mysql> select * from test where subject regexp "^[sp]h";
+----+----------+-------+---------+
| id | name     | score | subject |
+----+----------+-------+---------+
|  1 | xiaoming |    89 | shuxue  |
|  2 | xiaohong |    89 | shuxue  |
|  4 | xiaohong |    80 | physics |
|  6 | xiaoming |    80 | physics |
| 11 | xiaobai  |    80 | physics |
| 12 | xiaobai  |    80 | shuxue  |
| 14 | xiaohei  |    80 | shuxue  |
| 15 | xiaohei  |    80 | physics |
+----+----------+-------+---------+
8 rows in set (0.00 sec)



mysql> select * from test where subject regexp "^s|ph";
+----+----------+-------+---------+
| id | name     | score | subject |
+----+----------+-------+---------+
|  1 | xiaoming |    89 | shuxue  |
|  2 | xiaohong |    89 | shuxue  |
|  4 | xiaohong |    80 | physics |
|  6 | xiaoming |    80 | physics |
| 11 | xiaobai  |    80 | physics |
| 12 | xiaobai  |    80 | shuxue  |
| 14 | xiaohei  |    80 | shuxue  |
| 15 | xiaohei  |    80 | physics |
+----+----------+-------+---------+
8 rows in set (0.00 sec)



mysql> select * from test where name regexp "xiaoming|xiaohei";
+----+----------+-------+-----------+
| id | name     | score | subject   |
+----+----------+-------+-----------+
|  1 | xiaoming |    89 | shuxue    |
|  6 | xiaoming |    80 | physics   |
|  7 | xiaoming |    80 | astronaut |
|  8 | xiaoming |    80 | english   |
| 13 | xiaohei  |    80 | astronaut |
| 14 | xiaohei  |    80 | shuxue    |
| 15 | xiaohei  |    80 | physics   |
| 16 | xiaohei  |    80 | english   |
+----+----------+-------+-----------+
8 rows in set (0.00 sec)
mysql> select * from test;
+----+----------+-------+-----------+
| id | name     | score | subject   |
+----+----------+-------+-----------+
|  1 | xiaoming |    89 | shuxue    |
|  2 | xiaohong |    89 | shuxue    |
|  3 | xiaohong |    80 | english   |
|  4 | xiaohong |    80 | physics   |
|  5 | xiaohong |    80 | astronaut |
|  6 | xiaoming |    80 | physics   |
|  7 | xiaoming |    80 | astronaut |
|  8 | xiaoming |    80 | english   |
|  9 | xiaobai  |    80 | astronaut |
| 10 | xiaobai  |    80 | english   |
| 11 | xiaobai  |    80 | physics   |
| 12 | xiaobai  |    80 | shuxue    |
| 13 | xiaohei  |    80 | astronaut |
| 14 | xiaohei  |    80 | shuxue    |
| 15 | xiaohei  |    80 | physics   |
| 16 | xiaohei  |    80 | english   |
+----+----------+-------+-----------+
16 rows in set (0.00 sec)


mysql> update test set name="1.2xiaobai" where id=10;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0


mysql> select * from test;
+----+------------+-------+-----------+
| id | name       | score | subject   |
+----+------------+-------+-----------+
|  1 | xiaoming   |    89 | shuxue    |
|  2 | xiaohong   |    89 | shuxue    |
|  3 | xiaohong   |    80 | english   |
|  4 | xiaohong   |    80 | physics   |
|  5 | xiaohong   |    80 | astronaut |
|  6 | xiaoming   |    80 | physics   |
|  7 | xiaoming   |    80 | astronaut |
|  8 | xiaoming   |    80 | english   |
|  9 | xiaobai    |    80 | astronaut |
| 10 | 1.2xiaobai |    80 | english   |
| 11 | xiaobai    |    80 | physics   |
| 12 | xiaobai    |    80 | shuxue    |
| 13 | xiaohei    |    80 | astronaut |
| 14 | xiaohei    |    80 | shuxue    |
| 15 | xiaohei    |    80 | physics   |
| 16 | xiaohei    |    80 | english   |
+----+------------+-------+-----------+
16 rows in set (0.00 sec)

 
mysql> select * from test where name regexp "\.";
+----+------------+-------+---------+
| id | name       | score | subject |
+----+------------+-------+---------+
| 10 | 1.2xiaobai |    80 | english |
+----+------------+-------+---------+
1 row in set (0.00 sec)


mysql> update test set name="2.2xiaobai" where id=11;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0


mysql> select * from test where name regexp "[12]";
+----+------------+-------+---------+
| id | name       | score | subject |
+----+------------+-------+---------+
| 10 | 1.2xiaobai |    80 | english |
| 11 | 2.2xiaobai |    80 | physics |
+----+------------+-------+---------+
2 rows in set (0.00 sec)


mysql> update test set name="3xiaobai" where id=12;
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0


mysql> select * from test where name regexp "[12]";
+----+------------+-------+---------+
| id | name       | score | subject |
+----+------------+-------+---------+
| 10 | 1.2xiaobai |    80 | english |
| 11 | 2.2xiaobai |    80 | physics |
+----+------------+-------+---------+
2 rows in set (0.00 sec)


mysql> select * from test where name regexp "[12]\.";
+----+------------+-------+---------+
| id | name       | score | subject |
+----+------------+-------+---------+
| 10 | 1.2xiaobai |    80 | english |
| 11 | 2.2xiaobai |    80 | physics |
+----+------------+-------+---------+
2 rows in set (0.00 sec)


mysql> select * from test where name regexp "[123]\.";
+----+------------+-------+---------+
| id | name       | score | subject |
+----+------------+-------+---------+
| 10 | 1.2xiaobai |    80 | english |
| 11 | 2.2xiaobai |    80 | physics |
+----+------------+-------+---------+
2 rows in set (0.00 sec)


mysql> select * from test where name regexp "[123]";
+----+------------+-------+---------+
| id | name       | score | subject |
+----+------------+-------+---------+
| 10 | 1.2xiaobai |    80 | english |
| 11 | 2.2xiaobai |    80 | physics |
| 12 | 3xiaobai   |    80 | shuxue  |
+----+------------+-------+---------+
3 rows in set (0.00 sec)

原文地址:https://www.cnblogs.com/sea-stream/p/11306557.html