使用regexp關鍵字指定正規表示式的字元匹配模式。
mysql>
select
*from fruits where f_name regexp
'^a';+
------+--------+-------+
| f_id | f_name | price |
+------+--------+-------+
|1005|
+------+--------+-------+
1row
inset
(0.00 sec)
mysql>
select
*from fruits where f_name regexp
'^ba';+
------+--------+-------+
| f_id | f_name | price |
+------+--------+-------+
|103
| banana |12|
+------+--------+-------+
1row
inset
(0.00 sec)
mysql>
select
*from fruits where f_name regexp
'n$';+
------+------------+-------+
| f_id | f_name | price |
+------+------------+-------+
|102
| melon |8|
|106
| watermelon |20|
+------+------------+-------+
2rows
inset
(0.00 sec)
mysql>
select
*from fruits where f_name regexp
'le$';+
------+--------+-------+
| f_id | f_name | price |
+------+--------+-------+
|1005|
+------+--------+-------+
1row
inset
(0.00 sec)
mysql>
select
*from fruits where f_name regexp
'e.o';+
------+------------+-------+
| f_id | f_name | price |
+------+------------+-------+
|102
| melon |8|
|106
| watermelon |20|
+------+------------+-------+
2rows
inset
(0.00 sec)
mysql>
select
*from fruits where f_name regexp
'^ap*';+
------+--------+-------+
| f_id | f_name | price |
+------+--------+-------+
|1005|
|110
| alpp |10|
+------+--------+-------+
2rows
inset
(0.00 sec)
mysql>
select
*from fruits where f_name regexp
'^ap+';+
------+--------+-------+
| f_id | f_name | price |
+------+--------+-------+
|1005|
+------+--------+-------+
1row
inset
(0.00 sec)
匹配多個時使用』|'隔開
mysql>
select
*from fruits where f_name regexp
'ap|on';+
------+------------+-------+
| f_id | f_name | price |
+------+------------+-------+
|1005|
|102
| melon |8|
|106
| watermelon |20|
+------+------------+-------+
3rows
inset
(0.00 sec)
mysql>
select
*from fruits where f_name regexp
'[on]'
;//匹配有o或者有n的字串
+------+------------+-------+
| f_id | f_name | price |
+------+------------+-------+
|101
| orange |10|
|102
| melon |8|
|103
| banana |12|
|104
| mango |20|
|106
| watermelon |20|
+------+------------+-------+
5rows
inset
(0.00 sec)
[456] :為匹配有4或5或6的,也可以寫成[4-6],[a-z]即為所有字母。
[^xyz] :只要字元中有非xyz即可,如axyz。
[^xyz]$ :只要結尾字元不是xyz即可。
mysql>
select
*from fruits where f_name regexp
'[^on]$';+
------+--------+-------+
| f_id | f_name | price |
+------+--------+-------+
|1005|
|101
| orange |10|
|103
| banana |12|
|105
| water |5|
|107
| pear |
null|+
------+--------+-------+
5rows
inset
(0.00 sec)
a :a至少連續出現n次
a :a至少連續出現n次,少於m次
abc :ab後至少有n個c
(abc) :至少有n個abc
mysql>
select
*from fruits where f_name regexp
'an';+
------+--------+-------+
| f_id | f_name | price |
+------+--------+-------+
|101
| orange |10|
|103
| banana |12|
|104
| mango |20|
+------+--------+-------+
3rows
inset
(0.00 sec)
MYSQL 正規表示式查詢!
在使用select查詢的過程中,有時會用到正規表示式對結果進行查詢,將學習到的內容進行總結!一 語法結構如下 二 常用匹配方式進行示例說明 首先建立表student,表的結構如下 查詢student表中sname列已 王 開始的姓名 select sname from student where s...
mysql正規表示式 MySQL正規表示式
正規表示式是為複雜搜尋指定模式的強大方式。正規表示式描述了一組字串。最簡單的正規表示式是不含任何特殊字元的正規表示式。例如,正規表示式hello匹配hello。非平凡的正規表示式採用了特殊的特定結構,從而使得它們能夠與1個以上的字串匹配。例如,正規表示式hello word匹配字串hello或字串w...
Mysql的正規表示式查詢
select from info where name regexp l select from info where name regexp aaa select from info where name regexp c select from info where name regexp aa...