假设如果一个表中有许多值在表的列中包含空格,那么这是浪费空间。我们可以使用 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)
登录后复制
从上面的结果集中可以看到,所有的空格都被删除了,表格也被更新了。
以上就是我们如何使用 MySQL TRIM() 删除所有行中的空格并更新表?的详细内容,更多请关注每日运维网(www.mryunwei.com)其它相关文章!