假設如果一個表格中有許多值在表格的欄位中包含空格,那麼這是浪費空間。我們可以使用 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() 刪除所有行中的空格並更新表格?的詳細內容。更多資訊請關注PHP中文網其他相關文章!