如何从 MySQL 中的列中删除所有空格?
mysqlmysqli database更新于 2025/1/19 10:52:17
要从 MySQL 中的列中删除所有空格,可以使用 REPLACE() 函数。以下是语法 −
update yourTableName set yourColumnName=REPLACE(yourColumnName,' ', '' );
首先我们创建一个表 −
mysql> create table stripAllSpacesDemo -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(100) -> ); Query OK, 0 rows affected (0.56 sec)
下面是使用 insert 命令向表中插入记录的查询 −
mysql> insert into stripAllSpacesDemo(Name) values('Jo h n');
Query OK, 1 row affected (0.19 sec)
mysql> insert into stripAllSpacesDemo(Name) values(' Joh n');
Query OK, 1 row affected (0.16 sec)
mysql> insert into stripAllSpacesDemo(Name) values('Jo hn');
Query OK, 1 row affected (0.14 sec)
mysql> insert into stripAllSpacesDemo(Name) values('J ohn');
Query OK, 1 row affected (0.17 sec)
mysql> insert into stripAllSpacesDemo(Name) values('John ');
Query OK, 1 row affected (0.14 sec)
以下是使用 select 语句显示表中的所有记录的查询 −
mysql> select * from stripAllSpacesDemo;
这将产生以下输出 −
+----+-----------+ | Id | Name | +----+-----------+ | 1 | Jo h n | | 2 | Joh n | | 3 | Jo hn | | 4 | J ohn | | 5 | John | +----+-----------+ 5 rows in set (0.00 sec)
以下是从 MySQL − 中的列中删除所有空格的查询
mysql> update stripAllSpacesDemo set Name=REPLACE(Name,' ',''); Query OK, 5 rows affected (0.12 sec) Rows matched: 5 Changed: 5 Warnings: 0
让我们再次显示表中的所有记录,以检查是否已删除所有空格 −
mysql> select * from stripAllSpacesDemo;
这将产生以下输出 −
+----+------+ | Id | Name | +----+------+ | 1 | John | | 2 | John | | 3 | John | | 4 | John | | 5 | John | +----+------+ 5 rows in set (0.00 sec)
相关文章
有用资源
mysql 参考教程 - 该教程包含有关 mysql 的更多信息:https://www.cainiaomax.com/mysql/

