{"id":5204,"date":"2024-11-14T08:17:19","date_gmt":"2024-11-14T00:17:19","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5204\/"},"modified":"2024-11-14T08:17:19","modified_gmt":"2024-11-14T00:17:19","slug":"sql%e7%9a%84like%e8%af%ad%e5%8f%a5%e6%80%8e%e4%b9%88%e7%94%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5204\/","title":{"rendered":"sql\u7684like\u8bed\u53e5\u600e\u4e48\u7528"},"content":{"rendered":"<blockquote><p>\n  like \u8bed\u53e5\u7528\u4e8e\u5728 sql \u67e5\u8be2\u4e2d\u6839\u636e\u5b57\u7b26\u5e8f\u5217\u8fdb\u884c\u6a21\u7cca\u5339\u914d\u3002\u8bed\u6cd5 where column_name like &#8216;pattern&#8217;\u3002\u6a21\u5f0f\u5339\u914d\u5b57\u7b26\u5305\u62ec %\uff08\u5339\u914d\u4efb\u610f\u5b57\u7b26\uff09\u3001_\uff08\u5339\u914d\u5355\u4e2a\u5b57\u7b26\uff09\u3001[]\uff08\u5339\u914d\u65b9\u62ec\u53f7\u5185\u7684\u5b57\u7b26\uff09\u548c [^]\uff08\u5339\u914d\u65b9\u62ec\u53f7\u5916\u7684\u5b57\u7b26\uff09\u3002\u6a21\u7cca\u5339\u914d\u53ef\u7528\u4e8e\u5339\u914d\u7c7b\u4f3c\u4f46\u5e76\u4e0d\u5b8c\u5168\u76f8\u7b49\u7684\u5b57\u7b26\u4e32\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/24\/2024052420332861884.jpg\" class=\"aligncenter\" title=\"sql\u7684like\u8bed\u53e5\u600e\u4e48\u7528\u63d2\u56fe\" alt=\"sql\u7684like\u8bed\u53e5\u600e\u4e48\u7528\u63d2\u56fe\" \/><\/p>\n<p><strong>SQL \u4e2d LIKE \u8bed\u53e5\u8be6\u89e3<\/strong><\/p>\n<p><strong>\u4ec0\u4e48\u662f LIKE \u8bed\u53e5\uff1f<\/strong><\/p>\n<p>LIKE \u8bed\u53e5\u7528\u4e8e\u5728 SQL \u67e5\u8be2\u4e2d\u5339\u914d\u5305\u542b\u7279\u5b9a\u5b57\u7b26\u5e8f\u5217\u7684\u5b57\u7b26\u4e32\u3002\u5b83\u662f\u4e00\u79cd\u6a21\u5f0f\u5339\u914d\u64cd\u4f5c\u7b26\uff0c\u53ef\u8bc6\u522b\u6a21\u7cca\u5339\u914d\u6216\u90e8\u5206\u5339\u914d\u3002<\/p>\n<p><strong>\u8bed\u6cd5\uff1a<\/strong><\/p>\n<pre>WHERE 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\uff1a<\/strong>\u8981\u67e5\u8be2\u7684\u5b57\u7b26\u4e32\u5217<\/li>\n<li> <strong>pattern\uff1a<\/strong>\u8981\u5339\u914d\u7684\u5b57\u7b26\u5e8f\u5217\u6a21\u5f0f<\/li>\n<\/ul>\n<p><strong>\u6a21\u5f0f\u5339\u914d\u5b57\u7b26\uff1a<\/strong><\/p>\n<p>LIKE \u8bed\u53e5\u4f7f\u7528\u4ee5\u4e0b\u7279\u6b8a\u5b57\u7b26\u8fdb\u884c\u6a21\u5f0f\u5339\u914d\uff1a<\/p>\n<ul>\n<li> <strong>%\uff1a<\/strong>\u5339\u914d\u4efb\u610f\u6570\u91cf\u7684\u4efb\u4f55\u5b57\u7b26<\/li>\n<li> <strong>_\uff1a<\/strong>\u5339\u914d\u5355\u4e2a\u5b57\u7b26<\/li>\n<li> <strong>[]\uff1a<\/strong>\u5339\u914d\u65b9\u62ec\u53f7\u4e2d\u6307\u5b9a\u7684\u4efb\u4f55\u5b57\u7b26<\/li>\n<li> <strong>[^]\uff1a<\/strong>\u5339\u914d\u65b9\u62ec\u53f7\u4e2d\u672a\u6307\u5b9a\u7684\u4efb\u4f55\u5b57\u7b26<\/li>\n<\/ul>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>SELECT * FROM customers WHERE name LIKE '%Smith%';<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6b64\u67e5\u8be2\u5c06\u8fd4\u56de\u5305\u542b\u201cSmith\u201d\u5b57\u7b26\u4e32\uff08\u4f5c\u4e3a\u6574\u4e2a\u5355\u8bcd\u6216\u90e8\u5206\u5355\u8bcd\uff09\u7684\u6240\u6709\u5ba2\u6237\u8bb0\u5f55\u3002<\/p>\n<pre>SELECT * FROM products WHERE description LIKE 'smart%';<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6b64\u67e5\u8be2\u5c06\u8fd4\u56de\u63cf\u8ff0\u4e2d\u4ee5\u201csmart\u201d\u5f00\u5934\u7684\u6240\u6709\u4ea7\u54c1\u8bb0\u5f55\u3002<\/p>\n<pre>SELECT * FROM orders WHERE product_id LIKE '[1-9]%;<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6b64\u67e5\u8be2\u5c06\u8fd4\u56de\u4ea7\u54c1 ID \u4ee5 1 \u5230 9 \u4e4b\u95f4\u4efb\u4f55\u6570\u5b57\u5f00\u5934\u7684\u6240\u6709\u8ba2\u5355\u8bb0\u5f55\u3002<\/p>\n<p><strong>\u6a21\u7cca\u5339\u914d\uff1a<\/strong><\/p>\n<p>LIKE \u8bed\u53e5\u4e5f\u53ef\u7528\u4e8e\u6a21\u7cca\u5339\u914d\uff0c\u5373\u4e0d\u5b8c\u5168\u76f8\u7b49\u4f46\u76f8\u4f3c\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<pre>SELECT * FROM employees WHERE last_name LIKE 'John%';<\/pre>\n<p>  \u767b\u5f55\u540e\u590d\u5236   <\/p>\n<p>\u6b64\u67e5\u8be2\u5c06\u8fd4\u56de\u59d3\u6c0f\u4ee5\u201cJohn\u201d\u5f00\u5934\u7684\u6240\u6709\u5458\u5de5\u8bb0\u5f55\uff0c\u5373\u4f7f\u5176\u540e\u7f00\u4e0d\u540c\u3002<\/p>\n<p><strong>\u6ce8\u610f\u4e8b\u9879\uff1a<\/strong><\/p>\n<ul>\n<li>LIKE \u8bed\u53e5\u5bf9\u5927\u5c0f\u5199\u654f\u611f\uff0c\u9664\u975e\u4f7f\u7528 ESCAPE \u5b50\u53e5\u3002<\/li>\n<li>ESCAPE \u5b50\u53e5\u7528\u4e8e\u6307\u5b9a\u8f6c\u4e49\u5b57\u7b26\uff0c\u8be5\u5b57\u7b26\u53ef\u9632\u6b62\u6a21\u5f0f\u5339\u914d\u5b57\u7b26\u88ab\u89e3\u91ca\u4e3a\u5b9e\u9645\u5b57\u7b26\u3002<\/li>\n<\/ul>\n<p><strong>\u66ff\u4ee3\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528\u5176\u4ed6 SQL \u64cd\u4f5c\u7b26\u6765\u4ee3\u66ff LIKE \u8bed\u53e5\uff0c\u4f8b\u5982\uff1a<\/p>\n<ul>\n<li> <strong>=\uff1a<\/strong>\u5b8c\u5168\u76f8\u7b49\u5339\u914d<\/li>\n<li> <strong>\uff1a<\/strong>\u4e0d\u7b49\u4e8e\u5339\u914d<\/li>\n<li> <strong>IN\uff1a<\/strong>\u503c\u5217\u8868\u5339\u914d<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662fsql\u7684like\u8bed\u53e5\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>like \u8bed\u53e5\u7528\u4e8e\u5728 sql \u67e5\u8be2\u4e2d\u6839\u636e\u5b57\u7b26\u5e8f\u5217\u8fdb\u884c\u6a21\u7cca\u5339\u914d\u3002\u8bed\u6cd5 where column_name like &#8216;pattern&#8217;\u3002\u6a21\u5f0f\u5339\u914d\u5b57\u7b26\u5305\u62ec %\uff08\u5339\u914d\u4efb\u610f\u5b57\u7b26\uff09\u3001_\uff08\u5339\u914d\u5355\u4e2a\u5b57\u7b26\uff09\u3001[]\uff08\u5339\u914d\u65b9\u62ec\u53f7\u5185\u7684\u5b57\u7b26\uff09\u548c [^]\uff08\u5339\u914d\u65b9\u62ec\u53f7\u5916\u7684\u5b57\u7b26\uff09\u3002\u6a21\u7cca\u5339\u914d\u53ef\u7528\u4e8e\u5339\u914d\u7c7b\u4f3c\u4f46\u5e76\u4e0d\u5b8c\u5168\u76f8\u7b49\u7684\u5b57\u7b26\u4e32\u3002 SQL \u4e2d LIKE \u8bed\u53e5\u8be6\u89e3 \u4ec0\u4e48\u662f LIKE \u8bed\u53e5\uff1f LIKE \u8bed\u53e5\u7528\u4e8e\u5728 SQL \u67e5\u8be2\u4e2d\u5339\u914d\u5305\u542b\u7279\u5b9a\u5b57\u7b26\u5e8f\u5217\u7684\u5b57\u7b26\u4e32\u3002\u5b83\u662f\u4e00\u79cd\u6a21\u5f0f\u5339\u914d\u64cd\u4f5c\u7b26\uff0c\u53ef\u8bc6\u522b\u6a21\u7cca\u5339\u914d\u6216\u90e8\u5206\u5339\u914d\u3002 \u8bed\u6cd5\uff1a WHERE column_name LIKE &#8216;pattern&#8217; \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff1a column_name\uff1a\u8981\u67e5\u8be2\u7684\u5b57\u7b26\u4e32\u5217 pattern\uff1a\u8981\u5339\u914d\u7684\u5b57\u7b26\u5e8f\u5217\u6a21\u5f0f \u6a21\u5f0f\u5339\u914d\u5b57\u7b26\uff1a LIKE \u8bed\u53e5\u4f7f\u7528\u4ee5\u4e0b\u7279\u6b8a\u5b57\u7b26\u8fdb\u884c\u6a21\u5f0f\u5339\u914d\uff1a %\uff1a\u5339\u914d\u4efb\u610f\u6570\u91cf\u7684\u4efb\u4f55\u5b57\u7b26 _\uff1a\u5339\u914d\u5355\u4e2a\u5b57\u7b26 []\uff1a\u5339\u914d\u65b9\u62ec\u53f7\u4e2d\u6307\u5b9a\u7684\u4efb\u4f55\u5b57\u7b26 [^]\uff1a\u5339\u914d\u65b9\u62ec\u53f7\u4e2d\u672a\u6307\u5b9a\u7684\u4efb\u4f55\u5b57\u7b26 \u793a\u4f8b\uff1a SELECT * FROM customers WHERE name LIKE &#8216;%Smith%&#8217;; \u767b\u5f55\u540e\u590d\u5236 \u6b64\u67e5\u8be2\u5c06\u8fd4\u56de\u5305\u542b\u201cSmith\u201d\u5b57\u7b26\u4e32\uff08\u4f5c\u4e3a\u6574\u4e2a\u5355\u8bcd\u6216\u90e8\u5206\u5355\u8bcd\uff09\u7684\u6240\u6709\u5ba2\u6237\u8bb0\u5f55\u3002 SELECT * FROM products WHERE description LIKE [&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-5204","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5204","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=5204"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5204\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}