{"id":1118,"date":"2010-09-01T14:17:00","date_gmt":"2010-09-01T06:17:00","guid":{"rendered":"\/?p=1118"},"modified":"2010-09-01T14:17:00","modified_gmt":"2010-09-01T14:17:00","slug":"1118","status":"publish","type":"post","link":"https:\/\/blog.vimge.com\/archives\/mysql\/1118.html","title":{"rendered":"MySQL\u6269\u5c55\u6b63\u5219\u8868\u8fbe\u5f0f"},"content":{"rendered":"

\u7531MySQL\u63d0\u4f9b\u7684\u6a21\u5f0f\u5339\u914d\u7684\u5176\u4ed6\u7c7b\u578b\u662f\u4f7f\u7528\u6269\u5c55\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u5f53\u4f60\u5bf9\u8fd9\u7c7b\u6a21\u5f0f\u8fdb\u884c\u5339\u914d\u6d4b\u8bd5\u65f6\uff0c\u4f7f\u7528
REGEXP\u548cNOT REGEXP\u64cd\u4f5c\u7b26(\u6216RLIKE\u548cNOT RLIKE\uff0c\u5b83\u4eec\u662f\u540c\u4e49\u8bcd)\u3002
\u6269\u5c55\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u4e00\u4e9b\u5b57\u7b26\u662f\uff1a
“.”\u5339\u914d\u4efb\u4f55\u5355\u4e2a\u7684\u5b57\u7b26\u3002
\u4e00\u4e2a\u5b57\u7b26\u7c7b“[…]”\u5339\u914d\u5728\u65b9\u62ec\u53f7\u5185\u7684\u4efb\u4f55\u5b57\u7b26\u3002\u4f8b\u5982\uff0c“[abc]”\u5339\u914d“a”\u3001“b”\u6216“c”\u3002
\u4e3a\u4e86\u547d\u540d\u5b57\u7b26\u7684\u4e00\u4e2a\u8303\u56f4\uff0c\u4f7f\u7528\u4e00\u4e2a“-”\u3002“[a-z]”\u5339\u914d\u4efb\u4f55\u5c0f\u5199\u5b57\u6bcd\uff0c\u800c“[0-9]”\u5339\u914d\u4efb
\u4f55\u6570\u5b57\u3002
“ * ”\u5339\u914d\u96f6\u4e2a\u6216\u591a\u4e2a\u5728\u5b83\u524d\u9762\u7684\u4e1c\u897f\u3002\u4f8b\u5982\uff0c“x*”\u5339\u914d\u4efb\u4f55\u6570\u91cf\u7684“x”\u5b57\u7b26\uff0c“[0-9]*”
\u5339\u914d\u7684\u4efb\u4f55\u6570\u91cf\u7684\u6570\u5b57\uff0c\u800c“.*”\u5339\u914d\u4efb\u4f55\u6570\u91cf\u7684\u4efb\u4f55\u4e1c\u897f\u3002
\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u533a\u5206\u5927\u5c0f\u5199\u7684\uff0c\u4f46\u662f\u5982\u679c\u4f60\u5e0c\u671b\uff0c\u4f60\u80fd\u4f7f\u7528\u4e00\u4e2a\u5b57\u7b26\u7c7b\u5339\u914d\u4e24\u79cd\u5199\u6cd5\u3002\u4f8b\u5982\uff0c
“[aA]”\u5339\u914d\u5c0f\u5199\u6216\u5927\u5199\u7684“a”\u800c“[a-zA-Z]”\u5339\u914d\u4e24\u79cd\u5199\u6cd5\u7684\u4efb\u4f55\u5b57\u6bcd\u3002
\u5982\u679c\u5b83\u51fa\u73b0\u5728\u88ab\u6d4b\u8bd5\u503c\u7684\u4efb\u4f55\u5730\u65b9\uff0c\u6a21\u5f0f\u5c31\u5339\u914d(\u53ea\u8981\u4ed6\u4eec\u5339\u914d\u6574\u4e2a\u503c\uff0cSQL\u6a21\u5f0f\u5339\u914d)\u3002
\u4e3a\u4e86\u5b9a\u4f4d\u4e00\u4e2a\u6a21\u5f0f\u4ee5\u4fbf\u5b83\u5fc5\u987b\u5339\u914d\u88ab\u6d4b\u8bd5\u503c\u7684\u5f00\u59cb\u6216\u7ed3\u5c3e\uff0c\u5728\u6a21\u5f0f\u5f00\u59cb\u5904\u4f7f\u7528“^”\u6216\u5728\u6a21\u5f0f\u7684
\u7ed3\u5c3e\u7528“$”\u3002
\u4e3a\u4e86\u8bf4\u660e\u6269\u5c55\u6b63\u5219\u8868\u8fbe\u5f0f\u5982\u4f55\u5de5\u4f5c\uff0c\u4e0a\u9762\u6240\u793a\u7684LIKE\u67e5\u8be2\u5728\u4e0b\u9762\u4f7f\u7528REGEXP\u91cd\u5199\uff1a
\u4e3a\u4e86\u627e\u51fa\u4ee5“b”\u5f00\u5934\u7684\u540d\u5b57\uff0c\u4f7f\u7528“^”\u5339\u914d\u540d\u5b57\u7684\u5f00\u59cb\u5e76\u4e14“[bB]”\u5339\u914d\u5c0f\u5199\u6216\u5927\u5199\u7684“b”\uff1a
mysql> SELECT * FROM pet WHERE name REGEXP "^[bB]";
+——–+——–+———+——+————+————+
| name | owner | species | sex | birth | death |
+——–+——–+———+——+————+————+
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
| Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 |
+——–+——–+———+——+————+————+
\u4e3a\u4e86\u627e\u51fa\u4ee5“fy”\u7ed3\u5c3e\u7684\u540d\u5b57\uff0c\u4f7f\u7528“$”\u5339\u914d\u540d\u5b57\u7684\u7ed3\u5c3e\uff1a
mysql> SELECT * FROM pet WHERE name REGEXP "fy$";
+——–+——–+———+——+————+——-+
| name | owner | species | sex | birth | death |
+——–+——–+———+——+————+——-+
| Fluffy | Harold | cat | f | 1993-02-04 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
+——–+——–+———+——+————+——-+
\u4e3a\u4e86\u627e\u51fa\u5305\u542b\u4e00\u4e2a“w”\u7684\u540d\u5b57\uff0c\u4f7f\u7528“[wW]”\u5339\u914d\u5c0f\u5199\u6216\u5927\u5199\u7684“w”\uff1a
mysql> SELECT * FROM pet WHERE name REGEXP "[wW]";
+———-+——-+———+——+————+————+
| name | owner | species | sex | birth | death |
+———-+——-+———+——+————+————+
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Bowser | Diane | dog | m | 1989-08-31 | 1995-07-29 |
| Whistler | Gwen | bird | NULL | 1997-12-09 | NULL |
+———-+——-+———+——+————+————+
\u65e2\u7136\u5982\u679c\u4e00\u4e2a\u6b63\u89c4\u8868\u8fbe\u5f0f\u51fa\u73b0\u5728\u503c\u7684\u4efb\u4f55\u5730\u65b9\uff0c\u5176\u6a21\u5f0f\u5339\u914d\u4e86\uff0c\u5c31\u4e0d\u5fc5\u518d\u5148\u524d\u7684\u67e5\u8be2\u4e2d\u5728\u6a21\u5f0f\u7684\u4e24
\u65b9\u9762\u653e\u7f6e\u4e00\u4e2a\u901a\u914d\u7b26\u4ee5\u4f7f\u5f97\u5b83\u5339\u914d\u6574\u4e2a\u503c\uff0c\u5c31\u50cf\u5982\u679c\u4f60\u4f7f\u7528\u4e86\u4e00\u4e2aSQL\u6a21\u5f0f\u90a3\u6837\u3002
\u4e3a\u4e86\u627e\u51fa\u5305\u542b\u6b63\u597d5\u4e2a\u5b57\u7b26\u7684\u540d\u5b57\uff0c\u4f7f\u7528“^”\u548c“$”\u5339\u914d\u540d\u5b57\u7684\u5f00\u59cb\u548c\u7ed3\u5c3e\uff0c\u548c5\u4e2a“.”\u5b9e\u4f8b\u5728
\u4e24\u8005\u4e4b\u95f4\uff1a
mysql> SELECT * FROM pet WHERE name REGEXP "^…..$";
+——-+——–+———+——+————+——-+
| name | owner | species | sex | birth | death |
+——-+——–+———+——+————+——-+
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
+——-+——–+———+——+————+——-+
\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528“{n}”“\u91cd\u590dn\u6b21”\u64cd\u4f5c\u7b26\u91cd\u5199\u5148\u524d\u7684\u67e5\u8be2\uff1a
mysql> SELECT * FROM pet WHERE name REGEXP "^.{5}$";
+——-+——–+———+——+————+——-+
| name | owner | species | sex | birth | death |
+——-+——–+———+——+————+——-+
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
+——-+——–+———+——+————+——-+<\/p>\n","protected":false},"excerpt":{"rendered":"

\u7531MySQL\u63d0\u4f9b\u7684\u6a21\u5f0f\u5339\u914d\u7684\u5176\u4ed6\u7c7b\u578b\u662f\u4f7f\u7528\u6269\u5c55\u6b63\u5219\u8868\u8fbe\u5f0f\u3002\u5f53\u4f60\u5bf9\u8fd9\u7c7b\u6a21\u5f0f\u8fdb\u884c\u5339\u914d\u6d4b\u8bd5\u65f6\uff0c\u4f7f\u7528
REGEXP\u548cNOT REGEXP\u64cd\u4f5c\u7b26(\u6216RLIKE\u548cNOT RLIKE\uff0c\u5b83\u4eec\u662f\u540c\u4e49\u8bcd)\u3002
\u6269\u5c55\u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u4e00\u4e9b\u5b57\u7b26\u662f\uff1a
“.”\u5339\u914d\u4efb\u4f55\u5355\u4e2a\u7684\u5b57\u7b26\u3002
\u4e00\u4e2a\u5b57\u7b26\u7c7b“[…]”\u5339\u914d\u5728\u65b9\u62ec\u53f7\u5185\u7684\u4efb\u4f55\u5b57\u7b26\u3002\u4f8b\u5982\uff0c“[abc]”\u5339\u914d“a”\u3001“b”\u6216“c”\u3002
\u4e3a\u4e86\u547d\u540d\u5b57\u7b26\u7684\u4e00\u4e2a\u8303\u56f4\uff0c\u4f7f\u7528\u4e00\u4e2a“-”\u3002“[a-z]”\u5339\u914d\u4efb\u4f55\u5c0f\u5199\u5b57\u6bcd\uff0c\u800c“[0-9]”\u5339\u914d\u4efb
\u4f55\u6570\u5b57\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/posts\/1118"}],"collection":[{"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/comments?post=1118"}],"version-history":[{"count":0,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/posts\/1118\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/media?parent=1118"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/categories?post=1118"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.vimge.com\/wp-json\/wp\/v2\/tags?post=1118"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}