MySQL study notes. Query dataThree

Regular expression query

Query records starting with a specific character or string

The character'^' matches text starting with a specific character or string
1. Query records starting with x

mysql> SELECT *
    -> FROM fruits
    -> WHERE f_name REGEXP '^x';
+------+------+---------+---------+
| f_id | s_id | f_name  | f_price |
+------+------+---------+---------+
| b5   |  107 | xxxx    |    3.60 |
| m2   |  105 | xbabay  |    2.60 |
| t4   |  107 | xbababa |    3.60 |
+------+------+---------+---------+
3 rows in set (0.00 sec)

2Query records starting with xx

mysql> SELECT *
    -> FROM fruits
    -> WHERE f_name REGEXP '^xx';
+------+------+--------+---------+
| f_id | s_id | f_name | f_price |
+------+------+--------+---------+
| b5   |  107 | xxxx   |    3.60 |
+------+------+--------+---------+
1 row in set (0.00 sec)

Query records at the end of a specific character or string

The character $ matches text ending with a specific character or string
1. Query records ending with e

mysql> SELECT *
    -> FROM fruits
    -> WHERE f_name REGEXP 'e$';
+------+------+--------+---------+
| f_id | s_id | f_name | f_price |
+------+------+--------+---------+
| a1   |  101 | apple  |    5.20 |
| bs1  |  102 | orange |   11.20 |
| t2   |  102 | grape  |    5.30 |
+------+------+--------+---------+
3 rows in set (0.00 sec)

2. Query records ending with go

mysql> SELECT *
    -> FROM fruits
    -> WHERE f_name REGEXP 'go$';
+------+------+--------+---------+
| f_id | s_id | f_name | f_price |
+------+------+--------+---------+
| m1   |  106 | mngo   |   15.70 |
+------+------+--------+---------+
1 row in set (0.00 sec)

Use the symbol. to replace any character in the string

Characters. Match any character
1. Query records containing r and y

mysql> SELECT *
    -> FROM fruits
    -> WHERE f_name REGEXP 'r.y';
+------+------+------------+---------+
| f_id | s_id | f_name     | f_price |
+------+------+------------+---------+
| b1   |  101 | blackberry |   10.20 |
| b2   |  104 | berry      |    7.60 |
| c0   |  101 | cherry     |    3.20 |
+------+------+------------+---------+
3 rows in set (0.00 sec)

Use the symbols "*" and "+" to match multiple characters

"*" can match multiple characters in front, and "+" matches the characters in front at least once

mysql> SELECT *
    -> FROM fruits
    -> WHERE f_name REGEXP '^mn*';
+------+------+--------+---------+
| f_id | s_id | f_name | f_price |
+------+------+--------+---------+
| bs2  |  105 | melon  |    8.20 |
| m1   |  106 | mngo   |   15.70 |
+------+------+--------+---------+
2 rows in set (0.00 sec)
mysql> SELECT *
    -> FROM fruits
    -> WHERE f_name REGEXP '^mn+';
+------+------+--------+---------+
| f_id | s_id | f_name | f_price |
+------+------+--------+---------+
| m1   |  106 | mngo   |   15.70 |
+------+------+--------+---------+
1 row in set (0.00 sec)

Match the specified string

Regular expressions can match the specified string as long as the string is in the query text. If you want to match multiple strings, use the separator "|" to separate the multiple strings

1. Query records containing the string "ge"

mysql> SELECT *
    -> FROM fruits
    -> WHERE f_name REGEXP 'ge';
+------+------+--------+---------+
| f_id | s_id | f_name | f_price |
+------+------+--------+---------+
| bs1  |  102 | orange |   11.20 |
+------+------+--------+---------+
1 row in set (0.00 sec)

2. Query records containing "ge" or "na"

mysql> SELECT *
    -> FROM fruits
    -> WHERE f_name REGEXP 'ge|na';
+------+------+--------+---------+
| f_id | s_id | f_name | f_price |
+------+------+--------+---------+
| bs1  |  102 | orange |   11.20 |
| t1   |  102 | banana |   10.30 |
+------+------+--------+---------+
2 rows in set (0.00 sec)

Match any one of the specified characters

The square brackets "[]" designate a set of characters and only match any one of them.
1. Query records containing the letter "a" or "e"

mysql> SELECT *
    -> FROM fruits
    -> WHERE f_name REGEXP '[ae]';
+------+------+------------+---------+
| f_id | s_id | f_name     | f_price |
+------+------+------------+---------+
| a1   |  101 | apple      |    5.20 |
| a2   |  103 | apricot    |    2.20 |
| b1   |  101 | blackberry |   10.20 |
| b2   |  104 | berry      |    7.60 |
| bs1  |  102 | orange     |   11.20 |
| bs2  |  105 | melon      |    8.20 |
| c0   |  101 | cherry     |    3.20 |
| m2   |  105 | xbabay     |    2.60 |
| t1   |  102 | banana     |   10.30 |
| t2   |  102 | grape      |    5.30 |
| t4   |  107 | xbababa    |    3.60 |
+------+------+------------+---------+
11 rows in set (0.00 sec)

Use {n,} or {n,m} to specify the number of occurrences of the string

Query records where r occurs at least 2 times in a row

mysql> SELECT *
    -> FROM fruits
    -> WHERE f_name REGEXP 'r{2,}';
+------+------+------------+---------+
| f_id | s_id | f_name     | f_price |
+------+------+------------+---------+
| b1   |  101 | blackberry |   10.20 |
| b2   |  104 | berry      |    7.60 |
| c0   |  101 | cherry     |    3.20 |
+------+------+------------+---------+
3 rows in set (0.00 sec)

Match characters other than the specified characters

"[^Character Set]" matches any character not in the specified set,
matches records other than 101-104

mysql> SELECT *
    -> FROM fruits
    -> WHERE s_id REGEXP '[^101-104]';
+------+------+---------+---------+
| f_id | s_id | f_name  | f_price |
+------+------+---------+---------+
| a2   |  103 | apricot |    2.20 |
| b5   |  107 | xxxx    |    3.60 |
| bs1  |  102 | orange  |   11.20 |
| bs2  |  105 | melon   |    8.20 |
| m1   |  106 | mngo    |   15.70 |
| m2   |  105 | xbabay  |    2.60 |
| t1   |  102 | banana  |   10.30 |
| t2   |  102 | grape   |    5.30 |
| t4   |  107 | xbababa |    3.60 |
+------+------+---------+---------+
9 rows in set (0.00 sec)

Guess you like

Origin blog.csdn.net/qq_44862120/article/details/108980932