如何使用 MySQL TRIM() 删除所有行中的空白字符并更新表?
假设某张表中有许多值在表的列中带有空白字符,则这会浪费空间。我们可以使用 TRIM() 函数来移除所有行中的空白字符,并在一个查询中更新该表。以“Employee”为例,在其所有行中含有空白字符,将展示此概念 −
示例
mysql> Select * from Employee; +------+----------------------+----------------------+----------------------+ | Id | Name | Address | Department | +------+----------------------+----------------------+----------------------+ | 100 | Raman | Delhi | IT | | 101 | Mohan | Haryana | History | | 102 | Shyam | Chandigarh | ENGLISH | | 103 | Sukhjeet Singh | Patiala | Computer Engg. | | 104 | Bimal Roy | Calcutta | Computer Engg. | +------+----------------------+----------------------+----------------------+ 5 rows in set (0.00 sec)
我们可以从以上结果集中看到,Employee 表在所有行中都带有许多空白字符。可以通过以下查询移除并更新这些空白字符 −
mysql> Update Employee SET Id = TRIM(Id), Name = TRIM(Name), Address = TRIM(Address), DEPARTMENT = TRIM(Department); Query OK, 5 rows affected (0.24 sec) Rows matched: 5 Changed: 5 Warnings: 0 mysql> Select * from Employee; +------+----------------+------------+----------------+ | Id | Name | Address | Department | +------+----------------+------------+----------------+ | 100 | Raman | Delhi | IT | | 101 | Mohan | Haryana | History | | 102 | Shyam | Chandigarh | ENGLISH | | 103 | Sukhjeet Singh | Patiala | Computer Engg. | | 104 | Bimal Roy | Calcutta | Computer Engg. | +------+----------------+------------+----------------+ 5 rows in set (0.00 sec)
我们可以从以上结果集中看到,所有空白字符都已移除,并且该表已更新。
广告