MySQL 如何使用 REPLACE() 函数查找并替换数据以更新表?
我们知道,REPLACE() 函数用于将字符串中的子字符串替换为另一个子字符串。我们还可以将 REPLACE 函数与 UPDATE 语句结合使用,通过查找和替换数据来更新表。
示例
mysql> Update Student set Father_Name = REPLACE(Father_Name, 'Mr.','Shri '); Query OK, 5 rows affected (0.06 sec) Rows matched: 5 Changed: 5 Warnings: 0 mysql> Select Name, Father_Name from Student; +---------+-----------------+ | Name | Father_Name | +---------+-----------------+ | Gaurav | Shri Ramesh | | Aarav | Shri Sanjay | | Harshit | Shri Lovkesh | | Gaurav | Shri Ramchander | | Yashraj | Shri Mohan | +---------+-----------------+ 5 rows in set (0.00 sec)
上面的查询通过查找“Mr.”并用“Shri”替换它来更新 Father_name 列。
广告