{"id":5508,"date":"2024-11-14T10:32:49","date_gmt":"2024-11-14T02:32:49","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5508\/"},"modified":"2024-11-14T10:32:49","modified_gmt":"2024-11-14T02:32:49","slug":"mysql%e6%80%8e%e4%b9%88%e4%bd%bf%e7%94%a8%e6%a8%a1%e7%b3%8a%e6%9f%a5%e8%af%a2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5508\/","title":{"rendered":"mysql\u600e\u4e48\u4f7f\u7528\u6a21\u7cca\u67e5\u8be2"},"content":{"rendered":"<blockquote><p>\n  \u6a21\u7cca\u67e5\u8be2\u5141\u8bb8\u7528\u6237\u4f7f\u7528\u901a\u914d\u7b26\u5339\u914d\u6570\u636e\u5e93\u6570\u636e\u3002\u5728 mysql \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 % \u5339\u914d\u4efb\u610f\u5b57\u7b26\u6216 _ \u5339\u914d\u5355\u4e2a\u5b57\u7b26\u3002\u6a21\u7cca\u67e5\u8be2\u8bed\u6cd5\u4e3a\uff1aselect column_name from table_name where column_name like &#8216;pattern%&#8217;\u3002\u4f8b\u5982\uff0cselect username from users where username like &#8216;john%&#8217; \u67e5\u627e\u4ee5 &#8220;john&#8221; \u5f00\u5934\u7684\u7528\u6237\u540d\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/29\/2024052905272671217.jpg\" class=\"aligncenter\" title=\"mysql\u600e\u4e48\u4f7f\u7528\u6a21\u7cca\u67e5\u8be2\u63d2\u56fe\" alt=\"mysql\u600e\u4e48\u4f7f\u7528\u6a21\u7cca\u67e5\u8be2\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528 MySQL \u8fdb\u884c\u6a21\u7cca\u67e5\u8be2<\/strong><\/p>\n<p><strong>\u4ec0\u4e48\u662f\u6a21\u7cca\u67e5\u8be2\uff1f<\/strong><\/p>\n<p>\u6a21\u7cca\u67e5\u8be2\u662f\u4e00\u79cd\u6570\u636e\u5e93\u67e5\u8be2\uff0c\u5b83\u5141\u8bb8\u7528\u6237\u4f7f\u7528\u901a\u914d\u7b26\u6765\u5339\u914d\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u3002\u4e0e\u5176\u4f7f\u7528\u7cbe\u786e\u5339\u914d\uff0c\u6a21\u7cca\u67e5\u8be2\u53ef\u4ee5\u5339\u914d\u6587\u672c\u6216\u6570\u5b57\u7684\u4e00\u90e8\u5206\uff0c\u4f7f\u5176\u66f4\u7075\u6d3b\u548c\u901a\u7528\u3002<\/p>\n<p><strong>\u5728 MySQL \u4e2d\u8fdb\u884c\u6a21\u7cca\u67e5\u8be2<\/strong><\/p>\n<p>MySQL \u4e2d\u6709\u4e24\u79cd\u901a\u914d\u7b26\u53ef\u7528\u4e8e\u6a21\u7cca\u67e5\u8be2\uff1a<\/p>\n<ul>\n<li> <strong>%<\/strong> \u5339\u914d\u4efb\u610f\u6570\u91cf\u7684\u5b57\u7b26<\/li>\n<li> <strong>_<\/strong> \u5339\u914d\u5355\u4e2a\u5b57\u7b26<\/li>\n<\/ul>\n<p><strong>\u6a21\u7cca\u67e5\u8be2\u8bed\u6cd5<\/strong><\/p>\n<p>\u6a21\u7cca\u67e5\u8be2\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>SELECT column_name\nFROM table_name\nWHERE column_name LIKE 'pattern%';<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li> <strong>column_name<\/strong> \u662f\u8981\u641c\u7d22\u7684\u5217\u540d\u3002<\/li>\n<li> <strong>pattern<\/strong> \u662f\u8981\u5339\u914d\u7684\u6a21\u5f0f\u6216\u5b57\u7b26\u4e32\u3002<\/li>\n<li> <strong>%<\/strong> \u6216 <strong>_<\/strong> \u662f\u901a\u914d\u7b26\uff0c\u6307\u5b9a\u6a21\u7cca\u5339\u914d\u7684\u7c7b\u578b\u3002<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b<\/strong><\/p>\n<p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u5f20\u540d\u4e3a &#8220;users&#8221; \u7684\u8868\uff0c\u5176\u4e2d\u6709\u4e00\u5217\u540d\u4e3a &#8220;username&#8221;\u3002\u8981\u67e5\u627e\u4ee5 &#8220;john&#8221; \u5f00\u5934\u7684\u6240\u6709\u7528\u6237\u540d\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u6a21\u7cca\u67e5\u8be2\uff1a<\/p>\n<pre>SELECT username\nFROM users\nWHERE username LIKE 'john%';<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u5c06\u8fd4\u56de\u4ee5\u4e0b\u7ed3\u679c\uff1a<\/p>\n<pre>john\njohndoe\njohncena<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 % \u901a\u914d\u7b26<\/strong><\/p>\n<ul>\n<li> <strong>&#8216;%&#8217;<\/strong> \u5339\u914d\u4efb\u610f\u6570\u91cf\u7684\u5b57\u7b26\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u67e5\u8be2\u5c06\u8fd4\u56de\u6240\u6709\u4ee5 &#8220;john&#8221; \u5f00\u5934\u7684\u7528\u6237\u540d\uff1a<\/li>\n<\/ul>\n<pre>SELECT username\nFROM users\nWHERE username LIKE 'john%';<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>&#8216;%john%&#8217;<\/strong> \u5339\u914d\u5305\u542b &#8220;john&#8221; \u5b50\u5b57\u7b26\u4e32\u7684\u4efb\u4f55\u7528\u6237\u540d\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n<pre>SELECT username\nFROM users\nWHERE username LIKE '%john%';<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u4f7f\u7528 _ \u901a\u914d\u7b26<\/strong><\/p>\n<ul>\n<li> <strong>&#8216;_&#8217;<\/strong> \u5339\u914d\u5355\u4e2a\u5b57\u7b26\u3002\u4f8b\u5982\uff0c\u4ee5\u4e0b\u67e5\u8be2\u5c06\u8fd4\u56de\u6240\u6709\u7528\u6237\u540d\uff0c\u5176\u4e2d\u7b2c\u4e8c\u4e2a\u5b57\u7b26\u662f &#8220;o&#8221;\uff1a<\/li>\n<\/ul>\n<pre>SELECT username\nFROM users\nWHERE username LIKE '_o%';<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<ul>\n<li> <strong>&#8216;_john&#8217;<\/strong> \u5339\u914d\u4ee5 &#8220;j&#8221; \u5f00\u5934\uff0c\u7b2c\u4e8c\u4e2a\u5b57\u7b26\u662f &#8220;o&#8221;\uff0c\u7136\u540e\u8ddf\u4efb\u610f\u6570\u91cf\u7684\u5b57\u7b26\u7684\u4efb\u4f55\u7528\u6237\u540d\u3002\u4f8b\u5982\uff1a<\/li>\n<\/ul>\n<pre>SELECT username\nFROM users\nWHERE username LIKE '_john%';<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5176\u4ed6\u63d0\u793a<\/strong><\/p>\n<ul>\n<li>\u6a21\u7cca\u67e5\u8be2\u5bf9\u5927\u5c0f\u5199\u654f\u611f\uff0c\u56e0\u6b64\u8bf7\u786e\u4fdd\u4f7f\u7528\u6b63\u786e\u7684\u5b57\u6bcd\u5927\u5c0f\u5199\u3002<\/li>\n<li>\u4f7f\u7528\u901a\u914d\u7b26\u65f6\u8981\u5c0f\u5fc3\uff0c\u56e0\u4e3a\u8fd9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6027\u80fd\u95ee\u9898\u3002<\/li>\n<li>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u6a21\u7cca\u67e5\u8be2\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u600e\u4e48\u4f7f\u7528\u6a21\u7cca\u67e5\u8be2\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>\u6a21\u7cca\u67e5\u8be2\u5141\u8bb8\u7528\u6237\u4f7f\u7528\u901a\u914d\u7b26\u5339\u914d\u6570\u636e\u5e93\u6570\u636e\u3002\u5728 mysql \u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528 % \u5339\u914d\u4efb\u610f\u5b57\u7b26\u6216 _ \u5339\u914d\u5355\u4e2a\u5b57\u7b26\u3002\u6a21\u7cca\u67e5\u8be2\u8bed\u6cd5\u4e3a\uff1aselect column_name from table_name where column_name like &#8216;pattern%&#8217;\u3002\u4f8b\u5982\uff0cselect username from users where username like &#8216;john%&#8217; \u67e5\u627e\u4ee5 &#8220;john&#8221; \u5f00\u5934\u7684\u7528\u6237\u540d\u3002 \u5982\u4f55\u4f7f\u7528 MySQL \u8fdb\u884c\u6a21\u7cca\u67e5\u8be2 \u4ec0\u4e48\u662f\u6a21\u7cca\u67e5\u8be2\uff1f \u6a21\u7cca\u67e5\u8be2\u662f\u4e00\u79cd\u6570\u636e\u5e93\u67e5\u8be2\uff0c\u5b83\u5141\u8bb8\u7528\u6237\u4f7f\u7528\u901a\u914d\u7b26\u6765\u5339\u914d\u6570\u636e\u5e93\u4e2d\u7684\u6570\u636e\u3002\u4e0e\u5176\u4f7f\u7528\u7cbe\u786e\u5339\u914d\uff0c\u6a21\u7cca\u67e5\u8be2\u53ef\u4ee5\u5339\u914d\u6587\u672c\u6216\u6570\u5b57\u7684\u4e00\u90e8\u5206\uff0c\u4f7f\u5176\u66f4\u7075\u6d3b\u548c\u901a\u7528\u3002 \u5728 MySQL \u4e2d\u8fdb\u884c\u6a21\u7cca\u67e5\u8be2 MySQL \u4e2d\u6709\u4e24\u79cd\u901a\u914d\u7b26\u53ef\u7528\u4e8e\u6a21\u7cca\u67e5\u8be2\uff1a % \u5339\u914d\u4efb\u610f\u6570\u91cf\u7684\u5b57\u7b26 _ \u5339\u914d\u5355\u4e2a\u5b57\u7b26 \u6a21\u7cca\u67e5\u8be2\u8bed\u6cd5 \u6a21\u7cca\u67e5\u8be2\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a SELECT column_name FROM table_name WHERE column_name LIKE &#8216;pattern%&#8217;; \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff1a column_name \u662f\u8981\u641c\u7d22\u7684\u5217\u540d\u3002 pattern \u662f\u8981\u5339\u914d\u7684\u6a21\u5f0f\u6216\u5b57\u7b26\u4e32\u3002 % [&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-5508","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5508","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=5508"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5508\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5508"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5508"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5508"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}