首頁 >資料庫 >mysql教程 >在MySQL中,如何從整個列的值中刪除特定前綴並更新它們?

在MySQL中,如何從整個列的值中刪除特定前綴並更新它們?

WBOY
WBOY轉載
2023-09-16 18:17:11862瀏覽

在MySQL中,如何從整個列的值中刪除特定前綴並更新它們?

可以透過在列上套用 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中,如何從整個列的值中刪除特定前綴並更新它們?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除