如何使用 MySQL 的 REPLACE() 函数查找并替换数据来 UPADTE 表格?
众所周知,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 列。
广告