Sử dụng UPDATE kết hợp nhiều REPLACE(). Tạo bảng:
mysql> create table DemoTable -> ( -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> StudentName varchar(100), -> StudentCountryName varchar(100) -> ); Query OK, 0 rows affected (0.67 sec)
Chèn dữ liệu:
mysql> insert into DemoTable(StudentName,StudentCountryName) values('John','US'); Query OK, 1 row affected (0.15 sec)Hiển thị dữ liệu:
mysql select *from DemoTable;
Kết quả
+-----------+-------------+--------------------+ | StudentId | StudentName | StudentCountryName | +-----------+-------------+--------------------+ | 1 | John | US | +-----------+-------------+--------------------+ 1 row in set (0.00 sec)
Cập nhật đồng thời 2 cột:
mysql> update DemoTable -> set StudentName=replace(StudentName,'John','Chris'), -> StudentCountryName=replace(StudentCountryName,'US','UK'); Query OK, 1 row affected (0.69 sec) Rows matched: 1 Changed: 1 Warnings: 0
Kiểm tra lại:
mysql> select *from DemoTable;
Kết quả
+-----------+-------------+--------------------+ | StudentId | StudentName | StudentCountryName | +-----------+-------------+--------------------+ | 1 | Chris | UK | +-----------+-------------+--------------------+ 1 row in set (0.00 sec)