可以通過在列上應用 TRIM() 函數以及 MySQL UPDATE 語句來完成。下面的示例會更清楚地說明這一點。
示例
假設我們有一個表“Employee”,其前綴為“Dept.”,其中包含列“Department”的所有值如下 –
mysql> Select * from Employee; +------+----------------+------------+----------------------+ | Id | Name | Address | Department | +------+----------------+------------+----------------------+ | 100 | Raman | Delhi | IT Dept. | | 101 | Mohan | Haryana | History Dept. | | 102 | Shyam | Chandigarh | ENGLISH Dept. | | 103 | Sukhjeet Singh | Patiala | Computer Engg. Dept. | | 104 | Bimal Roy | Calcutta | Computer Engg. Dept. | +------+----------------+------------+----------------------+ 5 rows in set (0.01 sec)
登錄后復制
現在,下面的查詢將從“Department”列中刪除前綴“Dept.”,并更新表格。
mysql> Update Employee set Department = TRIM(Trailing 'Dept.' FROM Department); Query OK, 5 rows affected (0.10 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)
登錄后復制
上面的結果集顯示列值中沒有前綴“Dept.”。
以上就是在MySQL中,如何從整個列的值中刪除特定前綴并更新它們?的詳細內容,更多請關注www.92cms.cn其它相關文章!