如何更新 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”列的开头删除,并且表格也已更新。
广告