Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> MySQL

แทนที่ตัวอักษรในตาราง MySQL ด้วย REPLACE()

ใช้ REPLACE() แทนที่ตัวอักษรในคอลัมน์

สร้างตารางและเพิ่มข้อมูล

mysql> create table DemoTable ( Name varchar(20) ); 
Query OK, 0 rows affected (0.69 sec)
mysql> insert into DemoTable values('John Smitk'); 
Query OK, 1 row affected (0.09 sec) 
mysql> insert into DemoTable values('David Miller'); 
Query OK, 1 row affected (0.14 sec) 
mysql> insert into DemoTable values('Adam Smitk'); 
Query OK, 1 row affected (0.11 sec)

คิวรี่แทนที่ 'k' เป็น 'h'

mysql> update DemoTable set Name=replace(Name,'k','h'); 
Query OK, 2 rows affected (0.13 sec) 
Rows matched: 3  Changed: 2  Warnings: 0

ผลลัพธ์หลังอัปเดต

mysql> select * from DemoTable;
+--------------+ 
| Name         | 
+--------------+ 
| John Smith   | 
| David Miller | 
| Adam Smith   | 
+--------------+ 
3 rows in set (0.00 sec)

REPLACE(str, from_str, to_str) แทนที่ทุกครั้งที่พบ from_str ใน str ด้วย to_str | Case-sensitive | David Miller ไม่มี k จึงไม่เปลี่ยน