如何在移除列值中的特定字符串后更新MySQL表?
我们可以使用TRIM()函数以及UPDATE子句来更新MySQL表,并在列值中移除特定字符串。以下来自‘examination_btech’表的示例将使它更清晰:
示例
假设我们想要从‘Course’列的末尾删除值‘(CSE)’,并且也希望更新表,那么可以使用以下查询来实现:
mysql> Update examination_btech SET Course = TRIM(Trailing '(CSE)' FROM Course); Query OK, 10 rows affected (0.13 sec) mysql> Select * from examination_btech; +-----------+----------+--------+ | RollNo | Name | Course | +-----------+----------+--------+ | 201712001 | Rahul | B.tech | | 201712002 | Raman | B.tech | | 201712003 | Sahil | B.tech | | 201712004 | Shalini | B.tech | | 201712005 | Pankaj | B.tech | | 201712006 | Mohan | B.tech | | 201712007 | Yash | B.tech | | 201712008 | digvijay | B.tech | | 201712009 | Gurdas | B.tech | | 201712010 | Preeti | B.tech | +-----------+----------+--------+ 10 rows in set (0.00 sec)
从以上结果集中,可以清楚地看到‘(CSE)’已从‘course’列的末尾删除,并且表也已更新。
类似地,借助TRIM()函数,我们可以从开头删除字符串并更新表。
mysql> Update examination_btech SET RollNo = TRIM(Leading '201712' FROM RollNo); Query OK, 10 rows affected (0.04 sec) mysql> Select * from examination_btech; +--------+----------+--------+ | RollNo | Name | Course | +--------+----------+--------+ | 1 | Rahul | B.Tech | | 2 | Raman | B.Tech | | 3 | Sahil | B.Tech | | 4 | Shalini | B.Tech | | 5 | Pankaj | B.Tech | | 6 | Mohan | B.Tech | | 7 | Yash | B.Tech | | 8 | digvijay | B.Tech | | 9 | Gurdas | B.Tech | | 10 | Preeti | B.Tech | +--------+----------+--------+ 10 rows in set (0.00 sec)
从以上结果集中,可以清楚地看到‘201712’已从‘RollNo’列的开头删除,并且表也已更新。
广告