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

在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.”。

原文来自:www.php.cn
© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容