MySQL 更新查询删除空格?
mysqlmysqli database
您可以使用 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);