Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> MySQL

Tìm và thay thế chuỗi trong cột chứa đường dẫn file trong MySQL

Kỹ thuật

Sử dụng hàm REPLACE(column, 'old', 'new') trong câu lệnh UPDATE để thay đổi phần đường dẫn chung.

1. Tạo bảng

mysql> create table DemoTable (
    FolderLocation text
);
Query OK, 0 rows affected (0.80 sec)

2. Chèn dữ liệu

mysql> insert into DemoTable values('C/ProgramFiles/AllMySQLProgram');
mysql> insert into DemoTable values('C/ProgramFiles/JavaChatApplication');
mysql> insert into DemoTable values('C/ProgramFiles/Main/Image.png');

3. Xem dữ liệu

mysql> select * from DemoTable;
+------------------------------------+
| FolderLocation                     |
+------------------------------------+
| C/ProgramFiles/AllMySQLProgram     |
| C/ProgramFiles/JavaChatApplication |
| C/ProgramFiles/Main/Image.png      |
+------------------------------------+
3 rows in set (0.00 sec)

4. Cập nhật thay thế thư mục gốc

mysql> update DemoTable
    set FolderLocation = replace(FolderLocation, 'C/ProgramFiles', 'E/MyFolder/Details');
Query OK, 3 rows affected (0.14 sec)

5. Kết quả

mysql> select * from DemoTable;
+----------------------------------------+
| FolderLocation                         |
+----------------------------------------+
| E/MyFolder/Details/AllMySQLProgram     |
| E/MyFolder/Details/JavaChatApplication |
| E/MyFolder/Details/Main/Image.png      |
+----------------------------------------+
3 rows in set (0.00 sec)

Lưu ý: REPLACE thay thế tất cả lần xuất hiện. Nên backup trước khi chạy UPDATE hàng loạt.