基本概念
6、用萬用字元進行過濾
(1)like操作符
(2)百分號(%)萬用字元
select prod_id,prod_name
from products
where prod_name like 'jet%';
注:
select prod_id, prod_name
from products
where prod_name like '%anvil%';
select prod_name
from products
where prod_name like 's%e';
注:
(3)下劃線(_)萬用字元
select prod_id,prod_name
from products
where prod_name like '_ ton anvil';
(4)使用萬用字元的技巧
7、用正規表示式進行搜尋
(1) 正規表示式介紹
(2)使用mysql正規表示式
(3)基本字元匹配
select prod_name
from products
where prod_name regexp '1000'
order by prod_name;
select prod_name
from products
where prod_name regexp '.000'
order by prod_name;
注:
select prod_name
from products
where prod_name like '1000'
order by prod_name;
select prod_name
from products
where prod_name regexp '1000'
order by prod_name;
注:
(4)進行or匹配
select prod_name
from products
where prod_name regexp '1000|2000'
order by prod_name;
(5)匹配幾個字元之一
select prod_name
from products
where prod_name regexp '[123] ton'
order by prod_name;
注:
(6)匹配範圍
select prod_name
from products
where prod_name regexp '[1-5] ton'
order by prod_name;
(7)匹配特殊字元
select prod_name
from products
where prod_name regexp ' \\. '
order by prod_name;
元字元 說明
\\f 換頁
\\n 換行
\\r 回車
\\t 製表
\\v 縱向製表
\\\ \
注:
(8)匹配字元類
類 說明
[:alnum:] 任意字母和數字(同[a-za-z0-9])
[:alpha:] 任意字元(同[a-za-z])
[:blank:] 空格和製表(同[\\t])
[:cntrl:] ascii控制字元(ascii 0到31和127)
[:digit:] 任意數字(同[0-9])
[:graph:] 與[:print:]相同,但不包括空格
[:lower:] 任意小寫字母(同[a-z])
[:print:] 任意可列印字元
[:punct:] 既不在[:alnum:]又不在[:cntrl:]中的任意字元
[:space:] 包括空格在內的任意空白字元(同[\\f\\n\\r\\t\\v])
[:upper:] 任意大寫字母(同[a-z])
[:xdigit:] 任意十六進製制數字(同[a-fa-f0-9])
(9)匹配多個例項
元字元 說明
* 0個或多個匹配
+ 1個或多個匹配(等於)
? 0個或1個匹配(等於)
指定數目的匹配
不少於指定數目的匹配
匹配數目的範圍(m不超過255)
select prod_name
from products
where prod_name regexp ' \\([0-9] sticks?\\)'
order by prod_name;
select prod_name
from products
where prod_name regexp '[[:digit:]]'
order by prod_name;
上面的也可可以寫成:
select prod_name
from products
where prod_name regexp '[0-9][0-9][0-9][0-9]'
order by prod_name;
(10)定位符
元字元 說明
^ 文字的開始
$ 文字的結尾
[[:<:]] 詞的開始
[[:>:]] 詞的結尾
select prod_name
from products
where prod_name regexp '^[0-9\\.]'
order by prod_name;
注:
使regexp起類似like的作用:like和regexp 的不同在於,like匹配整個串而regexp匹配子串。利用定位符,通過用^開始每個表示式,用$結束每個表示式,可以使 regexp的作用與like一樣
【mysql必知必會】系列筆記:
【mysql必知必會1-4章】學習筆記day1
【mysql必知必會5-7章】學習筆記day2
《MySQL必知必會》學習筆記
本人在初學mysql語言,因害怕忘記,故把學習筆記寫到這個部落格上,以備查閱 所有種類的程式語言,文字編輯器,作業系統等都支援正規表示式。有見識的程式設計師和網路管理員已經關注作為他們技術工具重要內容的正規表示式很長時間了。正規表示式使用正規表示式語言建立,與任意語言一樣,正規表示式具有你必須學習的...
《MySQL必知必會》學習筆記
學習sql不是為了用它來幹活,只是出於興趣對它進行簡單的了解,寫下這篇學習筆記的目的也很簡單 幫助自己記憶。本次的學習筆記機遇linux centos 7 和mariadb。yum install mariadb mariadb server systemctl start mariadb syst...
mysql必知必 SQL必知必會學習筆記 一
資料庫基礎 資料庫 資料庫軟體 確切的說,資料庫軟體應稱為資料庫管理系統 dbms 資料庫是通過dbms建立和操作的容器 資料庫相當於檔案櫃 容器 表相當於檔案 同乙個資料庫不能存在相同的表名,不同的資料庫可以存在相同的表名 主鍵應滿足的條件 唯一性非空性 not null 主鍵列中的值不允許修改或...