首页 >数据库 >mysql教程 >在MySQL中,如何从整个列的值中删除特定前缀并更新它们?

在MySQL中,如何从整个列的值中删除特定前缀并更新它们?

WBOY
WBOY转载
2023-09-16 18:17:11848浏览

在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删除