We know that in the SQL, you can use like this predicate (expression) for fuzzy retrieval, and support for the%,?, _ and other placeholders.
However, the function of this fuzzy search has many limitations, simply too vague.
The REGEXP keyword is provided in MySQL to support regular expressions, of course, just some simple regular.
First, we construct some test data.
?
1 2 3 4 5 6 7 8 9 |
--Create a table use test; DROP TABLE IF EXISTS T_regcustomer; CREATE TABLE t_regcustomer (id int (a) auto_increment, name VARCHAR (256), Age INT (), PRIMARY KEY (ID)) Collate= ' Utf8_general_ci ' engine=innodb; |
Add some test data:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
1. The simplest query:
?
1 2 |
SELECT * from T_regcustomer; |
2. Specify Column name query
?
1 2 3 |
SELECT c.id, C.name, c.age from T_regcustomer C; |
3. Sort the query results
?
1 2 3 4 |
SELECT c.id, C.name, c.age from T_regcustomer C-order by C.age ASC; |
4. Like fuzzy search
% match any number of characters (0~n)
?
1 2 3 4 5 |
SELECT c.id, C.name, c.age from T_regcustomer c WHERE c.name like '% ROC% ' ORDER by C.age ASC; |
5. regexp keyword
. Match any one character
Note that there is no starting (^) and ending ($) qualifier, so as long as the rows that appear in the column are retrieved.
?
1 2 3 4 5 |
SELECT c.id, C.name, c.age from T_regcustomer C WHERE c.name REGEXP '. Peng. ' ORDER by C.age ASC; |
6. Regular Start qualifier
?
1 2 3 4 5 |
SELECT c.id, C.name, c.age from T_regcustomer C WHERE c.name REGEXP ' ^ King ' ORDER by C.age ASC; |
7. Case sensitive
?
1 2 3 4 5 |
SELECT c.id, C.name, c.age from T_regcustomer C WHERE c.name REGEXP the ' BINARY ' order by ^s ASC; |
8. Regular or operational
?
1 2 3 4 5 |
SELECT c.id, C.name, c.age from T_regcustomer C WHERE c.name REGEXP the ' BINARY ' order by A|s ASC; |
9. The group operation is regular
[123] indicates that 1, 2, 3 of these 3 digits appear
?
1 2 3 4 5 |
SELECT c.id, C.name, c.age from T_regcustomer C WHERE c.name REGEXP ' peng [BINARY] ' ORDER by 123 ASC; |
[1-9] matches 1, 2, 3 、.... 8, 9
?
1 2 3 4 5 |
SELECT c.id, C.name, c.age from T_regcustomer C WHERE c.name REGEXP ' peng [BINARY] ' ORDER by 1-9 ASC; |
10. Escape
Use
can be escaped. []()?-| As well as pagination, line-changing symbols, etc.
11. More Content
Please refer to the MySQL must-know-will 68-page regular expression