可以通过在列上应用 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
暂无评论内容