{"id":829,"date":"2012-12-25T15:19:10","date_gmt":"2012-12-25T07:19:10","guid":{"rendered":"http:\/\/www.kumouse.com\/?p=829"},"modified":"2012-12-25T15:19:10","modified_gmt":"2012-12-25T07:19:10","slug":"%e5%85%b3%e4%ba%8emysql%e7%9a%84%e6%a8%a1%e5%bc%8f%e5%8c%b9%e9%85%8d","status":"publish","type":"post","link":"https:\/\/www.kumouse.com\/?p=829","title":{"rendered":"\u5173\u4e8emysql\u7684\u6a21\u5f0f\u5339\u914d"},"content":{"rendered":"<p><a href=\"http:\/\/www.chinawin.net\/tag\/mysql\/\" target=\"_blank\" rel=\"nofollow\">MySQL<\/a>\u63d0\u4f9b\u6807\u51c6\u7684SQL\u6a21\u5f0f\u5339\u914d\uff0c\u4ee5\u53ca\u4e00\u79cd\u57fa\u4e8e\u8c61Unix\u5b9e\u7528\u7a0b\u5e8f\u5982vi\u3001grep\u548csed\u7684\u6269\u5c55\u6b63\u5219\u8868\u8fbe\u5f0f \u6a21\u5f0f\u5339\u914d\u7684\u683c\u5f0f\u3002 SQL\u7684\u6a21\u5f0f\u5339\u914d\u5141\u8bb8\u4f60\u4f7f\u7528\u201c_\u201d\u5339\u914d\u4efb\u4f55\u5355\u4e2a\u5b57\u7b26\uff0c\u800c\u201c%\u201d\u5339\u914d\u4efb\u610f\u6570\u76ee\u5b57\u7b26(\u5305\u62ec\u96f6\u4e2a\u5b57\u7b26)\u3002 \u5728 MySQL\u4e2d\uff0cSQL\u7684\u6a21\u5f0f\u7f3a\u7701\u662f\u5ffd\u7565\u5927\u5c0f\u5199\u7684\u3002\u4e0b\u9762\u663e\u793a\u4e00\u4e9b\u4f8b\u5b50\u3002\u6ce8\u610f\u5728\u4f60\u4f7f\u7528SQL\u6a21\u5f0f\u65f6\uff0c\u4f60\u4e0d \u80fd\u4f7f\u7528=\u6216!=\uff1b\u800c\u4f7f\u7528LIKE\u6216NOT LIKE\u6bd4\u8f83\u64cd\u4f5c\u7b26\u3002 \u4e3a\u4e86\u627e\u51fa\u4ee5\u201cb\u201d\u5f00\u5934\u7684\u540d\u5b57\uff1a<br \/>\nmysql&gt; SELECT * FROM pet WHERE name LIKE &#8220;b%&#8221;;<\/p>\n<p>\u4e3a\u4e86\u627e\u51fa\u4ee5\u201cfy\u201d\u7ed3\u5c3e\u7684\u540d\u5b57\uff1a<br \/>\nmysql&gt; SELECT * FROM pet WHERE name LIKE &#8220;%fy&#8221;; \u4e3a\u4e86\u627e\u51fa\u5305\u542b\u4e00\u4e2a\u201cw\u201d\u7684\u540d\u5b57\uff1a<br \/>\nmysql&gt; SELECT * FROM pet WHERE name LIKE &#8220;%w%&#8221;;<\/p>\n<p>\u4e3a\u4e86\u627e\u51fa\u5305\u542b\u6b63\u597d5\u4e2a\u5b57\u7b26\u7684\u540d\u5b57\uff0c\u4f7f\u7528\u201c_\u201d\u6a21\u5f0f\u5b57\u7b26\uff1a<br \/>\nmysql&gt; SELECT * FROM pet WHERE name LIKE &#8220;_____&#8221;;<\/p>\n<p>\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 \u201c.\u201d\u5339\u914d\u4efb\u4f55\u5355\u4e2a\u7684\u5b57\u7b26\u3002<br \/>\n\u4e00\u4e2a\u5b57\u7b26\u7c7b\u201c[&#8230;]\u201d\u5339\u914d\u5728\u65b9\u62ec\u53f7\u5185\u7684\u4efb\u4f55\u5b57\u7b26\u3002\u4f8b\u5982\uff0c\u201c[abc]\u201d\u5339\u914d\u201ca\u201d\u3001\u201cb\u201d\u6216\u201cc\u201d\u3002<br \/>\n\u4e3a\u4e86\u547d\u540d\u5b57\u7b26\u7684\u4e00\u4e2a\u8303\u56f4\uff0c\u4f7f\u7528\u4e00\u4e2a\u201c-\u201d\u3002\u201c[a-z]\u201d\u5339\u914d\u4efb\u4f55\u5c0f\u5199\u5b57\u6bcd\uff0c\u800c\u201c[0-9]\u201d\u5339\u914d\u4efb<br \/>\n\u4f55\u6570\u5b57\u3002<br \/>\n\u201c * \u201d\u5339\u914d\u96f6\u4e2a\u6216\u591a\u4e2a\u5728\u5b83\u524d\u9762\u7684\u4e1c\u897f\u3002\u4f8b\u5982\uff0c\u201cx*\u201d\u5339\u914d\u4efb\u4f55\u6570\u91cf\u7684\u201cx\u201d\u5b57\u7b26\uff0c\u201c[0-9]*\u201d<br \/>\n\u5339\u914d\u7684\u4efb\u4f55\u6570\u91cf\u7684\u6570\u5b57\uff0c\u800c\u201c.*\u201d\u5339\u914d\u4efb\u4f55\u6570\u91cf\u7684\u4efb\u4f55\u4e1c\u897f\u3002<br \/>\n\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<br \/>\n\u201c[aA]\u201d\u5339\u914d\u5c0f\u5199\u6216\u5927\u5199\u7684\u201ca\u201d\u800c\u201c[a-zA-Z]\u201d\u5339\u914d\u4e24\u79cd\u5199\u6cd5\u7684\u4efb\u4f55\u5b57\u6bcd\u3002<br \/>\n\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<br \/>\n\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\u201c^\u201d\u6216\u5728\u6a21\u5f0f\u7684<br \/>\n\u7ed3\u5c3e\u7528\u201c$\u201d\u3002<br \/>\n\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\u201cb\u201d\u5f00\u5934\u7684\u540d\u5b57\uff0c\u4f7f\u7528\u201c^\u201d\u5339\u914d\u540d\u5b57\u7684\u5f00\u59cb\u5e76\u4e14\u201c[bB]\u201d\u5339\u914d\u5c0f\u5199\u6216\u5927\u5199\u7684\u201cb\u201d\uff1a<br \/>\nmysql&gt; SELECT * FROM pet WHERE name REGEXP &#8220;^[bB]&#8221;;<\/p>\n<p>\u4e3a\u4e86\u627e\u51fa\u4ee5\u201cfy\u201d\u7ed3\u5c3e\u7684\u540d\u5b57\uff0c\u4f7f\u7528\u201c$\u201d\u5339\u914d\u540d\u5b57\u7684\u7ed3\u5c3e\uff1a<br \/>\nmysql&gt; SELECT * FROM pet WHERE name REGEXP &#8220;fy$&#8221;;<\/p>\n<p>\u4e3a\u4e86\u627e\u51fa\u5305\u542b\u4e00\u4e2a\u201cw\u201d\u7684\u540d\u5b57\uff0c\u4f7f\u7528\u201c[wW]\u201d\u5339\u914d\u5c0f\u5199\u6216\u5927\u5199\u7684\u201cw\u201d\uff1a<br \/>\nmysql&gt; SELECT * FROM pet WHERE name REGEXP &#8220;[wW]&#8221;;<\/p>\n<p>\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<br \/>\n\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<br \/>\n\u4e3a\u4e86\u627e\u51fa\u5305\u542b\u6b63\u597d5\u4e2a\u5b57\u7b26\u7684\u540d\u5b57\uff0c\u4f7f\u7528\u201c^\u201d\u548c\u201c$\u201d\u5339\u914d\u540d\u5b57\u7684\u5f00\u59cb\u548c\u7ed3\u5c3e\uff0c\u548c5\u4e2a\u201c.\u201d\u5b9e\u4f8b\u5728<br \/>\n\u4e24\u8005\u4e4b\u95f4\uff1a<br \/>\nmysql&gt; SELECT * FROM pet WHERE name REGEXP &#8220;^&#8230;..$&#8221;;<\/p>\n<p>\u4f60\u4e5f\u53ef\u4ee5\u4f7f\u7528\u201c{n}\u201d\u201c\u91cd\u590dn\u6b21\u201d\u64cd\u4f5c\u7b26\u91cd\u5199\u5148\u524d\u7684\u67e5\u8be2\uff1a<br \/>\nmysql&gt; SELECT * FROM pet WHERE name REGEXP &#8220;^.{5}$&#8221;;<\/p>\n<p>\u67e5\u627e\u6570\u5b57\u548c\u5176\u4ed6\u7684\u6a21\u7cca\u67e5\u8be2\u8bed\u53e5<br \/>\nSelect * from pet where name REGEXP &#8220;[^a-zA-Z].&#8221;;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MySQL\u63d0\u4f9b\u6807\u51c6\u7684SQL\u6a21\u5f0f\u5339\u914d\uff0c\u4ee5\u53ca\u4e00\u79cd\u57fa\u4e8e\u8c61Unix\u5b9e\u7528\u7a0b\u5e8f\u5982vi\u3001grep\u548csed\u7684\u6269\u5c55\u6b63\u5219\u8868\u8fbe\u5f0f \u6a21 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-829","post","type-post","status-publish","format-standard","hentry","category-linux"],"_links":{"self":[{"href":"https:\/\/www.kumouse.com\/index.php?rest_route=\/wp\/v2\/posts\/829","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kumouse.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kumouse.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kumouse.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kumouse.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=829"}],"version-history":[{"count":2,"href":"https:\/\/www.kumouse.com\/index.php?rest_route=\/wp\/v2\/posts\/829\/revisions"}],"predecessor-version":[{"id":831,"href":"https:\/\/www.kumouse.com\/index.php?rest_route=\/wp\/v2\/posts\/829\/revisions\/831"}],"wp:attachment":[{"href":"https:\/\/www.kumouse.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=829"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kumouse.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=829"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kumouse.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=829"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}