可以通过在列上应用 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.mryunwei.com)其它相关文章!