更新 MySQL 中的所有行并删除字符串中和字符串周围所有不必要的空格?
要删除不必要的空格,请在 MySQL 中使用 TRIM()。我们首先创建一个 -
mysql> create table DemoTable1405 -> ( -> FirstName varchar(20), -> LastName varchar(20) -> ); Query OK, 0 rows affected (0.55 sec)
使用 insert 来插入表中一些记录 -
mysql> insert into DemoTable1405 values(' Chris',' Brown '); Query OK, 1 row affected (0.20 sec) mysql> insert into DemoTable1405 values('David ',' Miller '); Query OK, 1 row affected (0.24 sec) mysql> insert into DemoTable1405 values(' Carol ',' Taylor '); Query OK, 1 row affected (0.19 sec)
使用 select 来显示表中的所有记录 -
mysql> select * from DemoTable1405;
这将产生以下输出 -
+------------------+----------------+ | FirstName | LastName | +------------------+----------------+ | Chris | Brown | | David | Miller | | Carol | Taylor | +------------------+----------------+ 3 rows in set (0.00 sec)
以下是删除所有不必要的空格并更新所有行的查询 -
mysql> update DemoTable1405 set FirstName=trim(FirstName),LastName=trim(LastName); Query OK, 3 rows affected (0.18 sec) Rows matched: 3 Changed: 3 Warnings: 0
让我们再次检查一下表记录 -
mysql> select * from DemoTable1405;
这将产生以下输出 -
+-----------+----------+ | FirstName | LastName | +-----------+----------+ | Chris | Brown | | David | Miller | | Carol | Taylor | +-----------+----------+ 3 rows in set (0.00 sec)
广告