MYSQL 正規表示式查詢!

2022-05-10 13:55:07 字數 2082 閱讀 1257

在使用select查詢的過程中,有時會用到正規表示式對結果進行查詢,將學習到的內容進行總結!

一 語法結構如下:

二 常用匹配方式進行示例說明

首先建立表student,表的結構如下:

1·^:查詢student表中sname列已『王』開始的姓名

select sname from student where sname regexp'^王

';    #查出結果:王麗.王芳

2·$:查詢student表中sname列已『文』結束的姓名 

select sname from student where sname regexp'文$

';  #查出結果:尚文

3. ·:查詢student表中sname的開始字元是『李』,後面不做限制的姓名

select sname from student where sname regexp'李.

'; #查出結果:李軍

4· [字元集合]:查詢student表中sbirthday列,年份中包含8或者5的年份

select sbirthday from student where sbirthday regexp

'[85]

';  #查出結果1798,1795

5.[^字元集合]:查詢student表中sbirthday列,年份除了1976的其餘年份

select sbirthday from student where sbirthday regexp

'[^1976]

';  #查出結果:1978,1974,1975

6. s1|s2|s3:查詢student表中sbirthday列,包含76|75|78的年份

select sbirthday from student where sbirthday regexp

'76|75|78

';   #查出結果:1976.1978.1975.1976

7·×:查詢student表中sname 列,『文』出現的sname

select sname from student where sname regexp'文*

';   #查出結果:尚文,說明:注意使用×是指查詢的字串可以有,也可以出現1次或者多次

8.+:查詢student表中sname 列,『文』出現的sname

select sname from student where sname regexp'文+

';  #查出結果:尚文,說明:注意使用+是指查詢的字串至少要出現1次

9.字元{n}:查詢student表中sname 列,『文』出現1次的sname

select sname from student where sname regexp'文

'; #查出結果:尚文,查詢出了文出現1次的姓名,將1改為2,將返回null,因為student表中sname列沒有文出現過2次的姓名

10. 字元{m,n}:查詢student表中sbirthday 列,『7』出現至少2次,最多不超過3的年份

select sbirthday from student where sbirthday regexp'7

';  #查出結果:1977,1977

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...

MySQL 使用正規表示式查詢

字元 匹配特定字元 select from fruits where f name regexp b 字元 特定字元結尾 select from fruits where f name regexp y 字元 代替字串中的任意乙個字元 select from fruits where f name ...