{"id":180704,"date":"2024-11-09T12:29:16","date_gmt":"2024-11-09T04:29:16","guid":{"rendered":"https:\/\/server.hk\/cnblog\/180704\/"},"modified":"2024-11-09T12:29:17","modified_gmt":"2024-11-09T04:29:17","slug":"%e6%b7%b1%e5%85%a5%e4%ba%86%e8%a7%a3mysql%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%af%94%e8%bc%83%e5%87%bd%e6%95%b8","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/180704\/","title":{"rendered":"\u6df1\u5165\u4e86\u89e3MySQL\u5b57\u7b26\u4e32\u6bd4\u8f03\u51fd\u6578"},"content":{"rendered":"<h1 id=\"%e6%b7%b1%e5%85%a5%e4%ba%86%e8%a7%a3mysql%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%af%94%e8%bc%83%e5%87%bd%e6%95%b8-hsxuZtrxjj\">\u6df1\u5165\u4e86\u89e3MySQL\u5b57\u7b26\u4e32\u6bd4\u8f03\u51fd\u6578<\/h1>\n<p>\u5728\u6578\u64da\u5eab\u7ba1\u7406\u7cfb\u7d71\u4e2d\uff0c\u5b57\u7b26\u4e32\u6bd4\u8f03\u662f\u975e\u5e38\u91cd\u8981\u7684\u4e00\u500b\u64cd\u4f5c\uff0c\u5c24\u5176\u662f\u5728\u4f7f\u7528MySQL\u6642\u3002MySQL\u63d0\u4f9b\u4e86\u591a\u7a2e\u5b57\u7b26\u4e32\u6bd4\u8f03\u51fd\u6578\uff0c\u9019\u4e9b\u51fd\u6578\u53ef\u4ee5\u5e6b\u52a9\u958b\u767c\u8005\u5728\u67e5\u8a62\u548c\u6578\u64da\u8655\u7406\u904e\u7a0b\u4e2d\u9032\u884c\u6709\u6548\u7684\u5b57\u7b26\u4e32\u6bd4\u8f03\u3002\u672c\u6587\u5c07\u6df1\u5165\u63a2\u8a0eMySQL\u4e2d\u7684\u5b57\u7b26\u4e32\u6bd4\u8f03\u51fd\u6578\uff0c\u5305\u62ec\u5176\u7528\u6cd5\u3001\u7279\u6027\u53ca\u5be6\u969b\u61c9\u7528\u793a\u4f8b\u3002<\/p>\n<h2 id=\"1-%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%af%94%e8%bc%83%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%a6%82%e5%bf%b5-hsxuZtrxjj\">1. \u5b57\u7b26\u4e32\u6bd4\u8f03\u7684\u57fa\u672c\u6982\u5ff5<\/h2>\n<p>\u5b57\u7b26\u4e32\u6bd4\u8f03\u662f\u6307\u5c0d\u5169\u500b\u6216\u591a\u500b\u5b57\u7b26\u4e32\u9032\u884c\u6bd4\u8f03\uff0c\u4ee5\u78ba\u5b9a\u5b83\u5011\u7684\u76f8\u7b49\u6027\u6216\u5927\u5c0f\u95dc\u4fc2\u3002\u5728MySQL\u4e2d\uff0c\u5b57\u7b26\u4e32\u6bd4\u8f03\u901a\u5e38\u6d89\u53ca\u5230\u4ee5\u4e0b\u5e7e\u500b\u65b9\u9762\uff1a<\/p>\n<ul>\n<li>\u76f8\u7b49\u6027\u6bd4\u8f03\uff1a\u6aa2\u67e5\u5169\u500b\u5b57\u7b26\u4e32\u662f\u5426\u76f8\u7b49\u3002<\/li>\n<li>\u5927\u5c0f\u6bd4\u8f03\uff1a\u78ba\u5b9a\u4e00\u500b\u5b57\u7b26\u4e32\u662f\u5426\u5927\u65bc\u6216\u5c0f\u65bc\u53e6\u4e00\u500b\u5b57\u7b26\u4e32\u3002<\/li>\n<li>\u6a21\u5f0f\u5339\u914d\uff1a\u4f7f\u7528\u901a\u914d\u7b26\u4f86\u5339\u914d\u5b57\u7b26\u4e32\u3002<\/li>\n<\/ul>\n<h2 id=\"2-mysql%e4%b8%ad%e7%9a%84%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%af%94%e8%bc%83%e5%87%bd%e6%95%b8-hsxuZtrxjj\">2. MySQL\u4e2d\u7684\u5b57\u7b26\u4e32\u6bd4\u8f03\u51fd\u6578<\/h2>\n<p>MySQL\u63d0\u4f9b\u4e86\u5e7e\u500b\u4e3b\u8981\u7684\u5b57\u7b26\u4e32\u6bd4\u8f03\u51fd\u6578\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e9b\u5e38\u7528\u7684\u51fd\u6578\uff1a<\/p>\n<h3 id=\"2-1-%e7%9b%b8%e7%ad%89%e9%81%8b%e7%ae%97%e7%ac%a6-hsxuZtrxjj\">2.1. = (\u76f8\u7b49\u904b\u7b97\u7b26)<\/h3>\n<p>\u6700\u57fa\u672c\u7684\u5b57\u7b26\u4e32\u6bd4\u8f03\u904b\u7b97\u7b26\u662f\u7b49\u65bc\u904b\u7b97\u7b26\uff08=\uff09\u3002\u5b83\u7528\u65bc\u6aa2\u67e5\u5169\u500b\u5b57\u7b26\u4e32\u662f\u5426\u76f8\u7b49\u3002<\/p>\n<pre><code>SELECT * FROM users WHERE username = 'admin';<\/code><\/pre>\n<h3 id=\"2-2-%e5%92%8c-%e5%a4%a7%e5%b0%8f%e9%81%8b%e7%ae%97%e7%ac%a6-hsxuZtrxjj\">2.2. &lt; \u548c &gt; (\u5927\u5c0f\u904b\u7b97\u7b26)<\/h3>\n<p>\u5927\u5c0f\u904b\u7b97\u7b26\u7528\u65bc\u6bd4\u8f03\u5b57\u7b26\u4e32\u7684\u5b57\u5178\u9806\u5e8f\u3002\u9019\u4e9b\u904b\u7b97\u7b26\u53ef\u4ee5\u7528\u4f86\u78ba\u5b9a\u4e00\u500b\u5b57\u7b26\u4e32\u662f\u5426\u5728\u53e6\u4e00\u500b\u5b57\u7b26\u4e32\u4e4b\u524d\u6216\u4e4b\u5f8c\u3002<\/p>\n<pre><code>SELECT * FROM products WHERE product_name &gt; 'A';<\/code><\/pre>\n<h3 id=\"2-3-like-%e6%a8%a1%e5%bc%8f%e5%8c%b9%e9%85%8d-hsxuZtrxjj\">2.3. LIKE (\u6a21\u5f0f\u5339\u914d)<\/h3>\n<p>LIKE\u904b\u7b97\u7b26\u7528\u65bc\u9032\u884c\u6a21\u5f0f\u5339\u914d\uff0c\u652f\u6301\u901a\u914d\u7b26\uff08%\u548c_\uff09\u3002\u9019\u5c0d\u65bc\u67e5\u8a62\u5305\u542b\u7279\u5b9a\u5b50\u5b57\u7b26\u4e32\u7684\u8a18\u9304\u975e\u5e38\u6709\u7528\u3002<\/p>\n<pre><code>SELECT * FROM customers WHERE email LIKE '%@gmail.com';<\/code><\/pre>\n<h3 id=\"2-4-strcmp-%e5%87%bd%e6%95%b8-hsxuZtrxjj\">2.4. STRCMP() \u51fd\u6578<\/h3>\n<p>STRCMP()\u51fd\u6578\u7528\u65bc\u6bd4\u8f03\u5169\u500b\u5b57\u7b26\u4e32\uff0c\u8fd4\u56de\u503c\u70ba0\u8868\u793a\u76f8\u7b49\uff0c\u8fd4\u56de\u503c\u5c0f\u65bc0\u8868\u793a\u7b2c\u4e00\u500b\u5b57\u7b26\u4e32\u5c0f\u65bc\u7b2c\u4e8c\u500b\u5b57\u7b26\u4e32\uff0c\u8fd4\u56de\u503c\u5927\u65bc0\u5247\u8868\u793a\u7b2c\u4e00\u500b\u5b57\u7b26\u4e32\u5927\u65bc\u7b2c\u4e8c\u500b\u5b57\u7b26\u4e32\u3002<\/p>\n<pre><code>SELECT STRCMP('abc', 'abc'); -- \u8fd4\u56de 0<\/code><\/pre>\n<h3 id=\"2-5-instr-%e5%87%bd%e6%95%b8-hsxuZtrxjj\">2.5. INSTR() \u51fd\u6578<\/h3>\n<p>INSTR()\u51fd\u6578\u7528\u65bc\u67e5\u627e\u4e00\u500b\u5b57\u7b26\u4e32\u5728\u53e6\u4e00\u500b\u5b57\u7b26\u4e32\u4e2d\u7684\u4f4d\u7f6e\uff0c\u8fd4\u56de\u503c\u70ba\u627e\u5230\u7684\u7d22\u5f15\u4f4d\u7f6e\uff0c\u82e5\u672a\u627e\u5230\u5247\u8fd4\u56de0\u3002<\/p>\n<pre><code>SELECT INSTR('Hello World', 'World'); -- \u8fd4\u56de 7<\/code><\/pre>\n<h2 id=\"3-%e5%ad%97%e7%ac%a6%e4%b8%b2%e6%af%94%e8%bc%83%e7%9a%84%e6%b3%a8%e6%84%8f%e4%ba%8b%e9%a0%85-hsxuZtrxjj\">3. \u5b57\u7b26\u4e32\u6bd4\u8f03\u7684\u6ce8\u610f\u4e8b\u9805<\/h2>\n<p>\u5728\u9032\u884c\u5b57\u7b26\u4e32\u6bd4\u8f03\u6642\uff0c\u6709\u5e7e\u500b\u6ce8\u610f\u4e8b\u9805\u9700\u8981\u8003\u616e\uff1a<\/p>\n<ul>\n<li><strong>\u5927\u5c0f\u5beb\u654f\u611f\u6027\uff1a<\/strong>MySQL\u7684\u5b57\u7b26\u4e32\u6bd4\u8f03\u662f\u5927\u5c0f\u5beb\u654f\u611f\u7684\uff0c\u9019\u53d6\u6c7a\u65bc\u5b57\u7b26\u96c6\u548c\u6392\u5e8f\u898f\u5247\u3002<\/li>\n<li><strong>\u5b57\u7b26\u96c6\u5f71\u97ff\uff1a<\/strong>\u4e0d\u540c\u7684\u5b57\u7b26\u96c6\u53ef\u80fd\u6703\u5f71\u97ff\u5b57\u7b26\u4e32\u7684\u6bd4\u8f03\u7d50\u679c\uff0c\u7279\u5225\u662f\u5728\u591a\u8a9e\u8a00\u74b0\u5883\u4e2d\u3002<\/li>\n<li><strong>\u6027\u80fd\u8003\u91cf\uff1a<\/strong>\u5728\u5927\u578b\u6578\u64da\u96c6\u4e0a\u9032\u884c\u5b57\u7b26\u4e32\u6bd4\u8f03\u6642\uff0c\u6027\u80fd\u53ef\u80fd\u6703\u53d7\u5230\u5f71\u97ff\uff0c\u5efa\u8b70\u4f7f\u7528\u7d22\u5f15\u4f86\u63d0\u9ad8\u67e5\u8a62\u6548\u7387\u3002<\/li>\n<\/ul>\n<h2 id=\"4-%e5%af%a6%e9%9a%9b%e6%87%89%e7%94%a8%e7%a4%ba%e4%be%8b-hsxuZtrxjj\">4. \u5be6\u969b\u61c9\u7528\u793a\u4f8b<\/h2>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u5be6\u969b\u61c9\u7528\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u5b57\u7b26\u4e32\u6bd4\u8f03\u51fd\u6578\u4f86\u67e5\u8a62\u7528\u6236\u8cc7\u6599\uff1a<\/p>\n<pre><code>SELECT * FROM users WHERE username LIKE 'user_%' AND STRCMP(status, 'active') = 0;<\/code><\/pre>\n<p>\u9019\u689d\u67e5\u8a62\u5c07\u8fd4\u56de\u6240\u6709\u4ee5&#8221;user_&#8221;\u958b\u982d\u4e14\u72c0\u614b\u70ba&#8221;active&#8221;\u7684\u7528\u6236\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-hsxuZtrxjj\">\u7e3d\u7d50<\/h2>\n<p>MySQL\u4e2d\u7684\u5b57\u7b26\u4e32\u6bd4\u8f03\u51fd\u6578\u70ba\u958b\u767c\u8005\u63d0\u4f9b\u4e86\u5f37\u5927\u7684\u5de5\u5177\u4f86\u8655\u7406\u548c\u67e5\u8a62\u5b57\u7b26\u4e32\u6578\u64da\u3002\u901a\u904e\u7406\u89e3\u548c\u904b\u7528\u9019\u4e9b\u51fd\u6578\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u9032\u884c\u6578\u64da\u7be9\u9078\u548c\u67e5\u8a62\u512a\u5316\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u6578\u64da\u8655\u7406\u7684\u61c9\u7528\u5834\u666f\uff0c\u9078\u64c7\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">VPS<\/a> \u65b9\u6848\u5c07\u6709\u52a9\u65bc\u63d0\u5347\u6574\u9ad4\u6027\u80fd\uff0c\u7279\u5225\u662f\u5728\u9999\u6e2f\u5730\u5340\u7684\u6578\u64da\u9700\u6c42\u4e2d\uff0c\u9078\u64c7\u5408\u9069\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a> \u5c07\u662f\u660e\u667a\u4e4b\u8209\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6df1\u5165\u4e86\u89e3MySQL\u5b57\u7b26\u4e32\u6bd4\u8f03\u51fd\u6578\uff0c\u638c\u63e1\u5982\u4f55\u6709\u6548\u6bd4\u8f03\u548c\u64cd\u4f5c\u5b57\u7b26\u4e32\u6578\u64da\uff0c\u63d0\u5347\u6578\u64da\u5eab\u67e5\u8a62\u6548\u7387\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-180704","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/180704","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=180704"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/180704\/revisions"}],"predecessor-version":[{"id":180705,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/180704\/revisions\/180705"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=180704"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=180704"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=180704"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}