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

Lấy phần trước ký tự xuất hiện cuối cùng trong MySQL

Cú pháp sử dụng REVERSE, SUBSTRING, INSTR:

update yourTableName set yourColumnName=REVERSE(SUBSTRING(REVERSE(yourColumnName), INSTR(REVERSE(yourColumnName), '.')));

Tạo bảng:

mysql> create table DemoTable -> ( -> Words text -> ); Query OK, 0 rows affected (0.51 sec)

Chèn dữ liệu:

mysql> insert into DemoTable values('Value1. Value2 .Value3.Value4.Value5'); Query OK, 1 row affected (0.22 sec)

Hiển thị dữ liệu:

mysql> select *from DemoTable;

Kết quả

+--------------------------------------+ | Words | +--------------------------------------+ | Value1. Value2 .Value3.Value4.Value5 | +--------------------------------------+ 1 row in set (0.00 sec)

Cập nhật lấy phần trước dấu . cuối cùng:

mysql> update DemoTable    -> set Words=REVERSE(SUBSTRING(REVERSE(Words), INSTR(REVERSE(Words),'.'))); Query OK, 1 row affected (0.13 sec) Rows matched: 1 Changed: 1 Warnings: 0

Kiểm tra lại:

mysql> select *from DemoTable;

Kết quả

+--------------------------------+ | Words | +--------------------------------+ | Value1. Value2 .Value3.Value4. | +--------------------------------+ 1 row in set (0.00 sec)