您可以使用TRIM()
函数删除空格。语法如下-
UPDATE yourTableName SET yourColumnName=TRIM(yourColumnName);
为了理解上述语法,让我们创建一个表。创建表的查询如下-
mysql> create table removeSpaceDemo -> ( -> Id int NOT NULL AUTO_INCREMENT, -> UserId varchar(20), -> UserName varchar(10), -> PRIMARY KEY(Id) -> );
使用insert命令在表中插入一些记录。查询如下-
mysql> insert into removeSpaceDemo(UserId,UserName) values('John1267 ','John'); mysql> insert into removeSpaceDemo(UserId,UserName) values(' 12Larry56','Larry'); mysql> insert into removeSpaceDemo(UserId,UserName) values(' David909','David'); mysql> insert into removeSpaceDemo(UserId,UserName) values('Bob912 ','Bob'); mysql> insert into removeSpaceDemo(UserId,UserName) values(' Sam123 ','Sam');
现在,您可以使用select语句显示表中的所有记录。查询如下-
mysql> select *from removeSpaceDemo;
以下是显示带空格的记录的输出:
+----+----------------+----------+ | Id | UserId | UserName | +----+----------------+----------+ | 1 | John1267 | John | | 2 | 12Larry56 | Larry | | 3 | David909 | David | | 4 | Bob912 | Bob | | 5 | Sam123 | Sam | +----+----------------+----------+ 5 rows in set (0.00 sec)
这是删除空格的查询-
mysql> update removeSpaceDemo set UserId=TRIM(UserId); Rows matched: 5 Changed: 5 Warnings: 0
现在再次检查表记录。查询如下-
mysql> select *from removeSpaceDemo;
以下是输出-
+----+-----------+----------+ | Id | UserId | UserName | +----+-----------+----------+ | 1 | John1267 | John | | 2 | 12Larry56 | Larry | | 3 | David909 | David | | 4 | Bob912 | Bob | | 5 | Sam123 | Sam | +----+-----------+----------+ 5 rows in set (0.00 sec)
您也可以使用RTRIM()
方法代替TRIM()
。语法如下-
UPDATE yourTableName SET yourColumnName=RTRIM(yourColumnName);