LIKE ...%
搜尋符合的紀錄,但對於例如開頭為數字的字串就無法滿足。MySQL 支援正規表示法,只要使用 REGEXP 就可以使用正規表示查詢,非常方便。正規表示運算如下:
RE字符 意義與範例 ^word 待搜尋的字串(word)在行首。 #範例:grep -n '^#' re.txt 搜尋行首為 # 開始的那一行。 word$ 待搜尋的字串(word)在行尾。 #範例:grep -n '!$' re.txt 將行尾為 ! 的那一行列印出來。 . 代表『任意一個』字符,一定是一個任意字符。 #範例:grep -n 'e.e' re.txt 搜尋的字串可以是 (eve) (eae) (eee) (e e), 但不能僅有 (ee)。 亦即 e 與 e 中間『一定』僅有一個字元,而空白字元也是字元。 \ 跳脫字符,將特殊符號的特殊意義去除。 #範例:grep -n \' re.txt 搜尋含有單引號 ' 的那一行。 * 重複零個或多個的前一個 RE 字符 #範例:grep -n 'ess*' re.txt 找出含有 (es) (ess) (esss) 等等的字串。 {n,m} 連續 n 到 m 個的『前一個 RE 字符』 若為 {n} 則是連續 n 個的前一個 RE 字符, 若是 {n,} 則是連續 n 個以上的前一個 RE 字符。 #範例:grep -n 'go{2,3}g' re.txt 在 g 與 g 之間有 2 個到 3 個的 o 存在的字串,亦即 (goog)(gooog) [ ] 在 [ ] 當中『謹代表一個待搜尋的字元』 #範例:grep -n 'g[ld]' re.txt 搜尋含有 (gl) 或 (gd) 的那一行 #範例:grep -n '[0-9]' re.txt 搜尋含有任意數字的那一行。 在字元集合 [ ] 中的減號 - 是代表兩個字元之間的所有連續字元。 [^]:^ 在 [ ] 內時, 代表的意義是『反向選擇』 #範例:grep -n 'oo[^t]' re.txt 搜尋的字串可以是 (oog) (ood) 但不能是 (oot)。 + 重複『一個或一個以上』的前一個 RE 字符 #範例:egrep -n 'go+d' re.txt 搜尋 (god) (good) (goood)... 等等的字串。 ? 『零個或一個』的前一個 RE 字符 #範例:egrep -n 'go?d' re.txt 搜尋 (gd) (god) 這兩個字串。 | 用或( or )的方式找出數個字串 #範例:egrep -n 'gd|good|dog' re.txt 搜尋 gd 、 good 或 dog 這三個字串。 ( ) 找出『群組』字串或作為『多個重複群組』的判別 #範例:egrep -n 'g(la|oo)d' 搜尋 (glad) 或 (good) 這兩個字串。 #範例:egrep -n 'A(xyz)+C' re.txt 找開頭是 A 結尾是 C ,中間有一個以上的 "xyz" 字串。
mariadb root@localhost:dyw> select * from dcount; +--------+--------+ | author | dcount | +--------+--------+ | dywang | 4 | | linda | 3 | | peter | 2 | | rita | 1 | | jenny | <null> | | sara | <null> | +--------+--------+ 6 rows in set Time: 0.021s
mariadb root@localhost:dyw> select * from dcount where author regexp '^r'; +--------+--------+ | author | dcount | +--------+--------+ | rita | 1 | +--------+--------+ 1 row in set Time: 0.065s
mariadb root@localhost:dyw> select * from dcount where author regexp 'a$'; +--------+--------+ | author | dcount | +--------+--------+ | linda | 3 | | rita | 1 | | sara | <null> | +--------+--------+ 3 rows in set Time: 0.021s
mariadb root@localhost:dyw> select * from dcount where author regexp '^[lpr]'; +--------+--------+ | author | dcount | +--------+--------+ | linda | 3 | | peter | 2 | | rita | 1 | +--------+--------+ 3 rows in set Time: 0.021s
mariadb root@localhost:dyw> select * from dcount where author regexp '^[lp]|ra'; +--------+--------+ | author | dcount | +--------+--------+ | linda | 3 | | peter | 2 | | sara | <null> | +--------+--------+ 3 rows in set Time: 0.022s