Yêu cầu
Tìm các bản ghi trong cột Title chứa đúng 3 dấu chấm phẩy. Sử dụng biểu thức chính quy REGEXP với mẫu (.*;){3}.
1. Tạo bảng
mysql> create table DemoTable (
Id int NOT NULL AUTO_INCREMENT PRIMARY KEY,
Title text
);
Query OK, 0 rows affected (0.88 sec)
2. Chèn dữ liệu
mysql> insert into DemoTable(Title) values('This is; a; MySQL;Tutorial');
mysql> insert into DemoTable(Title) values('Java is; an;Object Oriented');
mysql> insert into DemoTable(Title) values('MongoDB ; is;a; database');
3. Xem dữ liệu
mysql> select * from DemoTable;
+----+-----------------------------+ | Id | Title | +----+-----------------------------+ | 1 | This is; a; MySQL;Tutorial | | 2 | Java is; an;Object Oriented | | 3 | MongoDB ; is;a; database | +----+-----------------------------+ 3 rows in set (0.00 sec)
4. Truy vấn RegEx tìm 3 dấu chấm phẩy
mysql> select * from DemoTable where Title REGEXP '(.*;){3}';
Kết quả:
+----+----------------------------+ | Id | Title | +----+----------------------------+ | 1 | This is; a; MySQL;Tutorial | | 3 | MongoDB ; is;a; database | +----+----------------------------+ 2 rows in set (0.00 sec)
Giải thích: Mẫu (.*;){3} khớp với bất kỳ chuỗi nào chứa ít nhất 3 dấu chấm phẩy.