Hướng dẫn mysql replace string - chuỗi thay thế mysql
Trong bài này mình sẽ hướng dẫn cách sử dụng hàm Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.freetuts.net, không được copy dưới mọi hình thức. 1. Mô tảHàm 2. Cú phápCú pháp của hàm REPLACE( string, from_substring, to_substring ) Trong đó: Bài viết này được đăng tại [free tuts .net] mysql> SELECT REPLACE('freetuts.net', 'freetuts', 'myfreetuts'); Ket qua: 'myfreetuts.net' mysql> SELECT REPLACE('freetuts.net', 'e', '##########'); Ket qua: 'fr####################tuts.n##########t' mysql> SELECT REPLACE('freetuts.net', 'h', 'y'); Ket qua: 'freetuts.net' mysql> SELECT REPLACE('12345 12345', 4, 5); Ket qua: '12355 12355'2 Chuỗi nguồn. mysql> SELECT REPLACE('freetuts.net', 'freetuts', 'myfreetuts'); Ket qua: 'myfreetuts.net' mysql> SELECT REPLACE('freetuts.net', 'e', '##########'); Ket qua: 'fr####################tuts.n##########t' mysql> SELECT REPLACE('freetuts.net', 'h', 'y'); Ket qua: 'freetuts.net' mysql> SELECT REPLACE('12345 12345', 4, 5); Ket qua: '12355 12355'3 Các chuỗi con để tìm. Tất cả các lần xuất hiện của from_substring được tìm thấy trong chuỗi được thay thế bằng to_substring. mysql> SELECT REPLACE('freetuts.net', 'freetuts', 'myfreetuts'); Ket qua: 'myfreetuts.net' mysql> SELECT REPLACE('freetuts.net', 'e', '##########'); Ket qua: 'fr####################tuts.n##########t' mysql> SELECT REPLACE('freetuts.net', 'h', 'y'); Ket qua: 'freetuts.net' mysql> SELECT REPLACE('12345 12345', 4, 5); Ket qua: '12355 12355'4 Các chuỗi con thay thế. Tất cả các lần xuất hiện của from_substring được tìm thấy trong chuỗi được thay thế bằng to_substring. Lưu ý:
3. VersionChức năng
4. Ví dụmysql> SELECT REPLACE('freetuts.net', 'freetuts', 'myfreetuts'); Ket qua: 'myfreetuts.net' mysql> SELECT REPLACE('freetuts.net', 'e', '##########'); Ket qua: 'fr####################tuts.n##########t' mysql> SELECT REPLACE('freetuts.net', 'h', 'y'); Ket qua: 'freetuts.net' mysql> SELECT REPLACE('12345 12345', 4, 5); Ket qua: '12355 12355' 13.2.9 REPLACE Statement
mysql> SELECT REPLACE('freetuts.net', 'freetuts', 'myfreetuts'); Ket qua: 'myfreetuts.net' mysql> SELECT REPLACE('freetuts.net', 'e', '##########'); Ket qua: 'fr####################tuts.n##########t' mysql> SELECT REPLACE('freetuts.net', 'h', 'y'); Ket qua: 'freetuts.net' mysql> SELECT REPLACE('12345 12345', 4, 5); Ket qua: '12355 12355'9, except that if an old row in the table has the same value as a new row for a 0 or a 1 index, the old row is deleted before the new row is inserted. See Section 13.2.6, “INSERT Statement”.
3 inserts and replaces were deprecated in MySQL 5.6. In MySQL 8.0, 3 is not supported. The server recognizes but ignores the 3 keyword, handles the replace as a nondelayed replace, and generates an 6 warning: REPLACE DELAYED is no longer supported. The statement was converted to REPLACE. The 3 keyword is scheduled for removal in a future release. release. REPLACE DELAYED is no longer supported. The statement was converted to REPLACE. The 3 keyword is scheduled for removal in a future release. release. Note 0 or 1 index. Otherwise, it becomes equivalent to mysql> SELECT REPLACE('freetuts.net', 'freetuts', 'myfreetuts'); Ket qua: 'myfreetuts.net' mysql> SELECT REPLACE('freetuts.net', 'e', '##########'); Ket qua: 'fr####################tuts.n##########t' mysql> SELECT REPLACE('freetuts.net', 'h', 'y'); Ket qua: 'freetuts.net' mysql> SELECT REPLACE('12345 12345', 4, 5); Ket qua: '12355 12355'9, because there is no index to be used to determine whether a new row duplicates another. Values for all columns are taken from the values specified in the mysql> SELECT REPLACE('freetuts.net', 'freetuts', 'myfreetuts'); Ket qua: 'myfreetuts.net' mysql> SELECT REPLACE('freetuts.net', 'e', '##########'); Ket qua: 'fr####################tuts.n##########t' mysql> SELECT REPLACE('freetuts.net', 'h', 'y'); Ket qua: 'freetuts.net' mysql> SELECT REPLACE('12345 12345', 4, 5); Ket qua: '12355 12355'9. You cannot refer to values from the current row and use them in the new row. If you use an assignment such as 4 = 5 + 1, the reference to the column name on the right hand side is treated as DEFAULT( 5), so the assignment is equivalent to 4 = DEFAULT( 5) + 1. DEFAULT( , so the assignment is equivalent to 4 = DEFAULT( 5) + 1. In MySQL 8.0.19 and later, you can specify the column values that 0. To use mysql> SELECT REPLACE('freetuts.net', 'freetuts', 'myfreetuts'); Ket qua: 'myfreetuts.net' mysql> SELECT REPLACE('freetuts.net', 'e', '##########'); Ket qua: 'fr####################tuts.n##########t' mysql> SELECT REPLACE('freetuts.net', 'h', 'y'); Ket qua: 'freetuts.net' mysql> SELECT REPLACE('12345 12345', 4, 5); Ket qua: '12355 12355'9 and 3 privileges for the table. If a generated column is replaced explicitly, the only permitted value is 4. For information about generated columns, see Section 13.1.20.8, “CREATE TABLE and Generated Columns”. 6 clause with a list of comma-separated names of partitions, subpartitions, or both. As with mysql> SELECT REPLACE('freetuts.net', 'freetuts', 'myfreetuts'); Ket qua: 'myfreetuts.net' mysql> SELECT REPLACE('freetuts.net', 'e', '##########'); Ket qua: 'fr####################tuts.n##########t' mysql> SELECT REPLACE('freetuts.net', 'h', 'y'); Ket qua: 'freetuts.net' mysql> SELECT REPLACE('12345 12345', 4, 5); Ket qua: '12355 12355'9, if it is not possible to insert the new row into any of these partitions or subpartitions, the REPLACE statement fails with the error Found a row not matching the given partition set. For more information and examples, see Section 24.5, “Partition Selection”. Found a row not matching the given partition set. For more information and examples, see Section 24.5, “Partition Selection”. The The affected-rows count makes it easy to determine whether If you are using the C API, the affected-rows count can be obtained using the 2 function. You cannot replace into a table and select from the same table in a subquery. MySQL uses the following algorithm for 4):
Có thể trong trường hợp lỗi khóa trùng lặp, công cụ lưu trữ có thể thực hiện 6.Bởi vì kết quả của các câu lệnh 7 phụ thuộc vào thứ tự các hàng từ 8 và thứ tự này không thể luôn luôn được đảm bảo, có thể khi đăng nhập các câu lệnh này cho nguồn và bản sao để phân kỳ. Vì lý do này, các tuyên bố 7 được gắn cờ là không an toàn cho sao chép dựa trên tuyên bố. Các câu lệnh như vậy tạo ra cảnh báo trong nhật ký lỗi khi sử dụng chế độ dựa trên câu lệnh và được ghi vào nhật ký nhị phân bằng định dạng dựa trên hàng khi sử dụng chế độ 0. Xem thêm Phần & NBSP; 17.2.1.1, Ưu điểm và nhược điểm của bản sao dựa trên tuyên bố và dựa trên hàng.MySQL 8.0.19 và sau đó hỗ trợ 1 cũng như 8 với REPLACE , giống như với mysql> SELECT REPLACE('freetuts.net', 'freetuts', 'myfreetuts'); Ket qua: 'myfreetuts.net' mysql> SELECT REPLACE('freetuts.net', 'e', '##########'); Ket qua: 'fr####################tuts.n##########t' mysql> SELECT REPLACE('freetuts.net', 'h', 'y'); Ket qua: 'freetuts.net' mysql> SELECT REPLACE('12345 12345', 4, 5); Ket qua: '12355 12355'9. Xem Phần & NBSP; 13.2.6.1, Chèn chèn ... Chọn câu lệnh, để biết thêm thông tin và ví dụ. Khi sửa đổi một bảng hiện có không được phân vùng để phù hợp với phân vùng, hoặc, khi sửa đổi phân vùng của bảng đã được phân vùng, bạn có thể xem xét thay đổi khóa chính của bảng (xem Phần & NBSP; 24.6.1 Keys ”). Bạn nên lưu ý rằng, nếu bạn làm điều này, kết quả của các câu lệnh 6 sau:
Khi chúng tôi tạo bảng này và chạy các câu lệnh hiển thị trong máy khách MySQL, kết quả như sau:
Bây giờ chúng tôi tạo một bảng thứ hai gần giống với bảng thứ nhất, ngoại trừ khóa chính hiện bao gồm 2 cột, như được hiển thị ở đây (văn bản nhấn mạnh):
Khi chúng tôi chạy trên 7 cùng hai câu REPLACE như chúng tôi đã làm trên bảng 9 ban đầu, chúng tôi có được một kết quả khác:
Điều này là do thực tế là, khi chạy trên 7, cả hai giá trị cột REPLACE 1 và REPLACE 2 phải khớp với các giá trị của hàng hiện tại để hàng được thay thế; Nếu không, một hàng được chèn.
|