移除空格的 MySQL 更新查询?
可以使用 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) -> ); Query OK, 0 rows affected (0.81 sec)
使用 insert 命令插入一些记录到表中。查询如下:
mysql> insert into removeSpaceDemo(UserId,UserName) values('John1267 ','John'); Query OK, 1 row affected (0.25 sec) mysql> insert into removeSpaceDemo(UserId,UserName) values(' 12Larry56','Larry'); Query OK, 1 row affected (0.17 sec) mysql> insert into removeSpaceDemo(UserId,UserName) values(' David909','David'); Query OK, 1 row affected (0.18 sec) mysql> insert into removeSpaceDemo(UserId,UserName) values('Bob912 ','Bob'); Query OK, 1 row affected (0.13 sec) mysql> insert into removeSpaceDemo(UserId,UserName) values(' Sam123 ','Sam'); Query OK, 1 row affected (0.10 sec)
现在,可以使用 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); Query OK, 5 rows affected (0.33 sec) 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);
广告