对 MySQL 表中的所有记录使用 TRIM?
mysqlmysqli database更新于 2025/12/10 10:20:17
TRIM 用于删除前导和尾随空格。让我们首先创建一个表 −
mysql> create table DemoTable ( StudentName varchar(100) ); Query OK, 0 rows affected (0.64 sec)
使用 insert 命令在表中插入一些记录。在这里,我们插入了带有前导和尾随空格的记录 −
mysql> insert into DemoTable values(' Adam Smith ');
Query OK, 1 row affected (0.19 sec)
mysql> insert into DemoTable values(' David Miller ');
Query OK, 1 row affected (0.10 sec)
mysql> insert into DemoTable values(' Chris Brown ');
Query OK, 1 row affected (0.15 sec)
mysql> insert into DemoTable values(' Carol Taylor ');
Query OK, 1 row affected (0.10 sec)
使用 select 语句显示表中的所有记录 −
mysql> select *from DemoTable;
这将产生以下输出 −
+-----------------------------------+ | StudentName | +-----------------------------------+ | Adam Smith | David Miller | | Chris Brown | | Carol Taylor | +-----------------------------------+ 4 rows in set (0.00 sec)
以下是修剪 MySQL 表中的所有记录的查询 −
mysql> update DemoTable set StudentName=trim(StudentName); Query OK, 4 rows affected (0.24 sec) Rows matched: 4 Changed: 4 Warnings: 0
让我们再次检查表记录 −
mysql> select *from DemoTable;
这将产生以下输出。现在使用 TRIM() 成功删除了所有前导和尾随空格 &minuss;
+-----------------+ | StudentName | +-----------------+ | Adam Smith | | David Miller | | Chris Brown | | Carol Taylor | +-----------------+ 4 rows in set (0.00 sec)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

