如何使用 MySQL TRIM() 来从所有行中删除空格并更新表?
假设如果一张表中有多个值在表的列中有空格,那么它是空间浪费。我们可以使用 TRIM() 函数从所有行中删除空格,并在一个查询中更新表格。遵循“员工”中的示例,所有行中都有空格将展示该概念 -
示例
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)
我们可以从以上结果集中看到,所有空格已被删除并且表已被更新。
广告