通过使用TRIM()
函数和UPDATE子句从列的值中删除特定字符串后,我们可以更新MySQL表。以下是来自“ examination_btech”表中的示例,将使其更加清晰:
假设如果我们想从列'Course'的最后删除值'(CSE)'并且也想更新表,那么可以在以下查询的帮助下完成-
mysql> Update examination_btech SET Course = TRIM(Trailing '(CSE)' FROM Course); 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)”,并且该表也已更新。
同样,借助TRIM()
函数,我们可以从启动中删除字符串并更新表。
mysql> Update examination_btech SET RollNo = TRIM(Leading '201712' FROM RollNo); 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)
从上述结果集中,很明显,已从“ RollNo”列的开头删除了“ 201712”,并且该表也已更新。