{"id":5791,"date":"2024-11-14T09:00:11","date_gmt":"2024-11-14T01:00:11","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5791\/"},"modified":"2024-11-14T09:00:11","modified_gmt":"2024-11-14T01:00:11","slug":"mysql%e6%ad%a3%e5%88%99%e8%a1%a8%e8%be%be%e5%bc%8f%e6%80%8e%e4%b9%88%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5791\/","title":{"rendered":"mysql\u6b63\u5219\u8868\u8fbe\u5f0f\u600e\u4e48\u7528"},"content":{"rendered":"<blockquote><p>\n  mysql\u6b63\u5219\u8868\u8fbe\u5f0f\u7528\u4e8e\u6a21\u5f0f\u5339\u914d\uff0c\u901a\u8fc7regexp\u64cd\u4f5c\u7b26\u5c06\u6b63\u5219\u8868\u8fbe\u5f0f\u4e0e\u5b57\u7b26\u4e32\u8fdb\u884c\u6bd4\u8f83\u3002\u5176\u8bed\u6cd5\u4e3a\uff1aselect field from table where field regexp &#8216;pattern&#8217;\u3002mysql\u652f\u6301\u5404\u79cd\u6b63\u5219\u8868\u8fbe\u5f0f\u5143\u5b57\u7b26\uff0c\u5305\u62ec\u951a\u70b9\u5b57\u7b26\u3001\u6570\u91cf\u8bcd\u3001\u5206\u7ec4\u3001\u5b57\u7b26\u7c7b\u548c\u5143\u5b57\u7b26\u3002\u9ad8\u7ea7\u7528\u6cd5\u5305\u62ec\u5b50\u67e5\u8be2\u3001\u53cd\u5411\u5f15\u7528\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\u51fd\u6570\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/02\/2024060201371233320.jpg\" class=\"aligncenter\" title=\"mysql\u6b63\u5219\u8868\u8fbe\u5f0f\u600e\u4e48\u7528\u63d2\u56fe\" alt=\"mysql\u6b63\u5219\u8868\u8fbe\u5f0f\u600e\u4e48\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>MySQL \u6b63\u5219\u8868\u8fbe\u5f0f<\/strong><\/p>\n<p>\u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u7528\u4e8e\u5339\u914d\u5b57\u7b26\u4e32\u6a21\u5f0f\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u5728 MySQL \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u6a21\u5f0f\u5339\u914d\u3001\u5168\u6587\u641c\u7d22\u548c\u6570\u636e\u9a8c\u8bc1\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 MySQL \u6b63\u5219\u8868\u8fbe\u5f0f<\/strong><\/p>\n<p>\u5728 MySQL \u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 REGEXP \u64cd\u4f5c\u7b26\u5c06\u6b63\u5219\u8868\u8fbe\u5f0f\u4e0e\u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32\u8fdb\u884c\u6bd4\u8f83\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>SELECT field FROM table WHERE field REGEXP 'pattern';<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u6b63\u5219\u8868\u8fbe\u5f0f\u8bed\u6cd5<\/strong><\/p>\n<p>MySQL \u652f\u6301\u591a\u79cd\u6b63\u5219\u8868\u8fbe\u5f0f\u5143\u5b57\u7b26\uff0c\u5305\u62ec\uff1a<\/p>\n<ul>\n<li> <strong>\u951a\u70b9\u5b57\u7b26\uff1a<\/strong>^\u3001$\u3001<\/li>\n<li> <strong>\u6570\u91cf\u8bcd\uff1a<\/strong>{n,m}\u3001{n,}\u3001{n}\u3001?\u3001*\u3001+<\/li>\n<li> <strong>\u5206\u7ec4\uff1a<\/strong>()<\/li>\n<li> <strong>\u5b57\u7b26\u7c7b\uff1a<\/strong>[]\u3001[^]<\/li>\n<li> <strong>\u5143\u5b57\u7b26\uff1a<\/strong>.\u3001d\u3001s<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f7f\u7528 MySQL \u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u793a\u4f8b\uff1a<\/p>\n<ul>\n<li>\n<p><strong>\u5339\u914d\u4ee5 &#8220;a&#8221; \u5f00\u5934\u7684\u5b57\u7b26\u4e32\uff1a<\/strong><\/p>\n<pre>REGEXP '^a'<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p><strong>\u5339\u914d\u5305\u542b &#8220;xyz&#8221; \u5b50\u5b57\u7b26\u4e32\u7684\u5b57\u7b26\u4e32\uff1a<\/strong><\/p>\n<pre>REGEXP '.*xyz.*'<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<li>\n<p><strong>\u5339\u914d\u4ee5\u6570\u5b57\u7ed3\u5c3e\u7684\u5b57\u7b26\u4e32\uff1a<\/strong><\/p>\n<pre>REGEXP '.*d$'<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/li>\n<\/ul>\n<p><strong>\u9ad8\u7ea7\u7528\u6cd5<\/strong><\/p>\n<p>MySQL \u8fd8\u652f\u6301\u9ad8\u7ea7\u6b63\u5219\u8868\u8fbe\u5f0f\u529f\u80fd\uff0c\u5982\uff1a<\/p>\n<ul>\n<li> <strong>\u5b50\u67e5\u8be2\uff1a<\/strong>\u4f7f\u7528 (?:) \u5bf9\u5b50\u6a21\u5f0f\u8fdb\u884c\u975e\u6355\u83b7\u5206\u7ec4\u3002<\/li>\n<li> <strong>\u53cd\u5411\u5f15\u7528\uff1a<\/strong>\u4f7f\u7528 \u5f15\u7528\u5148\u524d\u7684\u5206\u7ec4\u3002<\/li>\n<li> <strong>\u6b63\u5219\u8868\u8fbe\u5f0f\u51fd\u6570\uff1a<\/strong>REGEXP_MATCH() \u548c REGEXP_REPLACE() \u51fd\u6570\u53ef\u66f4\u8f7b\u677e\u5730\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/li>\n<\/ul>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<ul>\n<li>MySQL \u4f7f\u7528 SQL \u6807\u51c6\u6b63\u5219\u8868\u8fbe\u5f0f\u8bed\u6cd5\u3002<\/li>\n<li>\u6b63\u5219\u8868\u8fbe\u5f0f\u67e5\u8be2\u53ef\u80fd\u4f1a\u5f71\u54cd\u6027\u80fd\uff0c\u5e94\u8c28\u614e\u4f7f\u7528\u3002<\/li>\n<li>\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u65f6\u5e94\u8003\u8651\u8f93\u5165\u9a8c\u8bc1\u548c\u6570\u636e\u5b8c\u6574\u6027\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u6b63\u5219\u8868\u8fbe\u5f0f\u600e\u4e48\u7528\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u7f51\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>mysql\u6b63\u5219\u8868\u8fbe\u5f0f\u7528\u4e8e\u6a21\u5f0f\u5339\u914d\uff0c\u901a\u8fc7regexp\u64cd\u4f5c\u7b26\u5c06\u6b63\u5219\u8868\u8fbe\u5f0f\u4e0e\u5b57\u7b26\u4e32\u8fdb\u884c\u6bd4\u8f83\u3002\u5176\u8bed\u6cd5\u4e3a\uff1aselect field from table where field regexp &#8216;pattern&#8217;\u3002mysql\u652f\u6301\u5404\u79cd\u6b63\u5219\u8868\u8fbe\u5f0f\u5143\u5b57\u7b26\uff0c\u5305\u62ec\u951a\u70b9\u5b57\u7b26\u3001\u6570\u91cf\u8bcd\u3001\u5206\u7ec4\u3001\u5b57\u7b26\u7c7b\u548c\u5143\u5b57\u7b26\u3002\u9ad8\u7ea7\u7528\u6cd5\u5305\u62ec\u5b50\u67e5\u8be2\u3001\u53cd\u5411\u5f15\u7528\u548c\u6b63\u5219\u8868\u8fbe\u5f0f\u51fd\u6570\u3002 MySQL \u6b63\u5219\u8868\u8fbe\u5f0f \u6b63\u5219\u8868\u8fbe\u5f0f\u662f\u7528\u4e8e\u5339\u914d\u5b57\u7b26\u4e32\u6a21\u5f0f\u7684\u5f3a\u5927\u5de5\u5177\uff0c\u5728 MySQL \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8fdb\u884c\u6a21\u5f0f\u5339\u914d\u3001\u5168\u6587\u641c\u7d22\u548c\u6570\u636e\u9a8c\u8bc1\u3002 \u5982\u4f55\u4f7f\u7528 MySQL \u6b63\u5219\u8868\u8fbe\u5f0f \u5728 MySQL \u4e2d\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528 REGEXP \u64cd\u4f5c\u7b26\u5c06\u6b63\u5219\u8868\u8fbe\u5f0f\u4e0e\u8981\u5339\u914d\u7684\u5b57\u7b26\u4e32\u8fdb\u884c\u6bd4\u8f83\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a SELECT field FROM table WHERE field REGEXP &#8216;pattern&#8217;; \u767b\u5f55\u540e\u590d\u5236 \u6b63\u5219\u8868\u8fbe\u5f0f\u8bed\u6cd5 MySQL \u652f\u6301\u591a\u79cd\u6b63\u5219\u8868\u8fbe\u5f0f\u5143\u5b57\u7b26\uff0c\u5305\u62ec\uff1a \u951a\u70b9\u5b57\u7b26\uff1a^\u3001$\u3001 \u6570\u91cf\u8bcd\uff1a{n,m}\u3001{n,}\u3001{n}\u3001?\u3001*\u3001+ \u5206\u7ec4\uff1a() \u5b57\u7b26\u7c7b\uff1a[]\u3001[^] \u5143\u5b57\u7b26\uff1a.\u3001d\u3001s \u793a\u4f8b \u4ee5\u4e0b\u662f\u4e00\u4e9b\u4f7f\u7528 MySQL \u6b63\u5219\u8868\u8fbe\u5f0f\u7684\u793a\u4f8b\uff1a \u5339\u914d\u4ee5 &#8220;a&#8221; \u5f00\u5934\u7684\u5b57\u7b26\u4e32\uff1a REGEXP &#8216;^a&#8217; \u767b\u5f55\u540e\u590d\u5236 \u5339\u914d\u5305\u542b &#8220;xyz&#8221; \u5b50\u5b57\u7b26\u4e32\u7684\u5b57\u7b26\u4e32\uff1a REGEXP &#8216;.*xyz.*&#8217; \u767b\u5f55\u540e\u590d\u5236 \u5339\u914d\u4ee5\u6570\u5b57\u7ed3\u5c3e\u7684\u5b57\u7b26\u4e32\uff1a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-5791","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5791","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=5791"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5791\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5791"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5791"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5791"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}