假设一个表中有很多值在表的列中都带有空格,那么这就是空间的浪费。我们可以使用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); 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)
从上面的结果集中我们可以看到,所有空格都已删除,并且表已更新。