为此,将UPDATE命令与REGEXP一起使用。让我们首先创建一个表-
mysql> create table DemoTable2023 -> ( -> StreetNumber varchar(100) -> );
使用插入命令在表中插入一些记录-
mysql> insert into DemoTable2023 values('7'); mysql> insert into DemoTable2023 values('1'); mysql> insert into DemoTable2023 values('AUS-100'); mysql> insert into DemoTable2023 values('US-101');
使用select语句显示表中的所有记录-
mysql> select *from DemoTable2023;
这将产生以下输出-
+--------------+ | StreetNumber | +--------------+ | 7 | | 1 | | AUS-100 | | US-101 | +--------------+ 4 rows in set (0.00 sec)
这是使用正则表达式更新表的查询-
mysql> update DemoTable2023 -> set StreetNumber=concat('Street',StreetNumber) -> where StreetNumber regexp'^[0-9]{1,2}$'; Rows matched: 2 Changed: 2 Warnings: 0
让我们再次检查表记录-
mysql> select *from DemoTable2023;
这将产生以下输出-
+--------------+ | StreetNumber | +--------------+ | Street7 | | Street1 | | AUS-100 | | US-101 | +--------------+ 4 rows in set (0.00 sec)