如何在 MySQL 表中的所有记录上使用 TRIM?
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)
广告