{"id":168425,"date":"2024-11-06T12:16:17","date_gmt":"2024-11-06T04:16:17","guid":{"rendered":"https:\/\/server.hk\/cnblog\/168425\/"},"modified":"2024-11-06T12:16:18","modified_gmt":"2024-11-06T04:16:18","slug":"sql-%e5%84%aa%e5%8c%96-%e9%9a%b1%e5%bc%8f%e5%ad%97%e7%ac%a6%e7%b7%a8%e7%a2%bc%e8%bd%89%e6%8f%9b","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/168425\/","title":{"rendered":"SQL \u512a\u5316 &#8211; \u96b1\u5f0f\u5b57\u7b26\u7de8\u78bc\u8f49\u63db"},"content":{"rendered":"<h1 id=\"sql-%e5%84%aa%e5%8c%96-%e9%9a%b1%e5%bc%8f%e5%ad%97%e7%ac%a6%e7%b7%a8%e7%a2%bc%e8%bd%89%e6%8f%9b-aGsyZWJxOK\">SQL \u512a\u5316 &#8211; \u96b1\u5f0f\u5b57\u7b26\u7de8\u78bc\u8f49\u63db<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u64da\u9a45\u52d5\u4e16\u754c\u4e2d\uff0c\u6578\u64da\u5eab\u7684\u6027\u80fd\u5c0d\u65bc\u61c9\u7528\u7a0b\u5e8f\u7684\u6574\u9ad4\u6548\u7387\u81f3\u95dc\u91cd\u8981\u3002SQL \u512a\u5316\u662f\u63d0\u5347\u6578\u64da\u5eab\u6027\u80fd\u7684\u4e00\u500b\u91cd\u8981\u65b9\u9762\uff0c\u5176\u4e2d\u96b1\u5f0f\u5b57\u7b26\u7de8\u78bc\u8f49\u63db\u662f\u4e00\u500b\u5e38\u88ab\u5ffd\u8996\u4f46\u537b\u5f71\u97ff\u6027\u80fd\u7684\u56e0\u7d20\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u96b1\u5f0f\u5b57\u7b26\u7de8\u78bc\u8f49\u63db\u7684\u6982\u5ff5\u3001\u5176\u5c0d SQL \u67e5\u8a62\u6027\u80fd\u7684\u5f71\u97ff\uff0c\u4ee5\u53ca\u5982\u4f55\u9032\u884c\u512a\u5316\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af%e9%9a%b1%e5%bc%8f%e5%ad%97%e7%ac%a6%e7%b7%a8%e7%a2%bc%e8%bd%89%e6%8f%9b%ef%bc%9f-aGsyZWJxOK\">\u4ec0\u9ebc\u662f\u96b1\u5f0f\u5b57\u7b26\u7de8\u78bc\u8f49\u63db\uff1f<\/h2>\n<p>\u96b1\u5f0f\u5b57\u7b26\u7de8\u78bc\u8f49\u63db\u662f\u6307\u5728 SQL \u67e5\u8a62\u4e2d\uff0c\u6578\u64da\u5eab\u7cfb\u7d71\u81ea\u52d5\u5c07\u4e00\u7a2e\u5b57\u7b26\u7de8\u78bc\u8f49\u63db\u70ba\u53e6\u4e00\u7a2e\u5b57\u7b26\u7de8\u78bc\u7684\u904e\u7a0b\u3002\u9019\u901a\u5e38\u767c\u751f\u5728\u6578\u64da\u5eab\u4e2d\u7684\u5b57\u7b26\u985e\u578b\u4e0d\u5339\u914d\u6642\u3002\u4f8b\u5982\uff0c\u7576\u4e00\u500b\u67e5\u8a62\u6d89\u53ca\u5230\u4e0d\u540c\u5b57\u7b26\u96c6\u7684\u5217\u6642\uff0c\u6578\u64da\u5eab\u53ef\u80fd\u6703\u81ea\u52d5\u9032\u884c\u8f49\u63db\uff0c\u4ee5\u78ba\u4fdd\u67e5\u8a62\u80fd\u5920\u6b63\u78ba\u57f7\u884c\u3002<\/p>\n<h2 id=\"%e9%9a%b1%e5%bc%8f%e5%ad%97%e7%ac%a6%e7%b7%a8%e7%a2%bc%e8%bd%89%e6%8f%9b%e7%9a%84%e5%bd%b1%e9%9f%bf-aGsyZWJxOK\">\u96b1\u5f0f\u5b57\u7b26\u7de8\u78bc\u8f49\u63db\u7684\u5f71\u97ff<\/h2>\n<p>\u96b1\u5f0f\u5b57\u7b26\u7de8\u78bc\u8f49\u63db\u53ef\u80fd\u6703\u5c0d SQL \u67e5\u8a62\u7684\u6027\u80fd\u7522\u751f\u8ca0\u9762\u5f71\u97ff\uff0c\u4e3b\u8981\u8868\u73fe\u5728\u4ee5\u4e0b\u5e7e\u500b\u65b9\u9762\uff1a<\/p>\n<ul>\n<li><strong>\u6027\u80fd\u4e0b\u964d\uff1a<\/strong>\u96b1\u5f0f\u8f49\u63db\u9700\u8981\u984d\u5916\u7684\u8a08\u7b97\u8cc7\u6e90\uff0c\u9019\u53ef\u80fd\u5c0e\u81f4\u67e5\u8a62\u57f7\u884c\u6642\u9593\u7684\u589e\u52a0\u3002<\/li>\n<li><strong>\u7d22\u5f15\u5931\u6548\uff1a<\/strong>\u5982\u679c\u67e5\u8a62\u4e2d\u7684\u5217\u6d89\u53ca\u96b1\u5f0f\u8f49\u63db\uff0c\u6578\u64da\u5eab\u53ef\u80fd\u7121\u6cd5\u4f7f\u7528\u7d22\u5f15\uff0c\u5f9e\u800c\u5c0e\u81f4\u5168\u8868\u6383\u63cf\uff0c\u9032\u4e00\u6b65\u5f71\u97ff\u6027\u80fd\u3002<\/li>\n<li><strong>\u932f\u8aa4\u548c\u4e0d\u4e00\u81f4\u6027\uff1a<\/strong>\u96b1\u5f0f\u8f49\u63db\u53ef\u80fd\u5c0e\u81f4\u6578\u64da\u4e0d\u4e00\u81f4\uff0c\u7279\u5225\u662f\u5728\u591a\u8a9e\u8a00\u74b0\u5883\u4e2d\uff0c\u9019\u53ef\u80fd\u6703\u5f15\u767c\u932f\u8aa4\u6216\u4e0d\u6b63\u78ba\u7684\u67e5\u8a62\u7d50\u679c\u3002<\/li>\n<\/ul>\n<h2 id=\"%e5%a6%82%e4%bd%95%e5%84%aa%e5%8c%96%e9%9a%b1%e5%bc%8f%e5%ad%97%e7%ac%a6%e7%b7%a8%e7%a2%bc%e8%bd%89%e6%8f%9b-aGsyZWJxOK\">\u5982\u4f55\u512a\u5316\u96b1\u5f0f\u5b57\u7b26\u7de8\u78bc\u8f49\u63db<\/h2>\n<p>\u70ba\u4e86\u6e1b\u5c11\u96b1\u5f0f\u5b57\u7b26\u7de8\u78bc\u8f49\u63db\u5c0d SQL \u67e5\u8a62\u6027\u80fd\u7684\u5f71\u97ff\uff0c\u53ef\u4ee5\u63a1\u53d6\u4ee5\u4e0b\u5e7e\u500b\u512a\u5316\u63aa\u65bd\uff1a<\/p>\n<h3 id=\"1-%e7%a2%ba%e4%bf%9d%e5%ad%97%e7%ac%a6%e9%9b%86%e4%b8%80%e8%87%b4%e6%80%a7-aGsyZWJxOK\">1. \u78ba\u4fdd\u5b57\u7b26\u96c6\u4e00\u81f4\u6027<\/h3>\n<p>\u5728\u8a2d\u8a08\u6578\u64da\u5eab\u6642\uff0c\u61c9\u78ba\u4fdd\u6240\u6709\u76f8\u95dc\u8868\u548c\u5217\u4f7f\u7528\u76f8\u540c\u7684\u5b57\u7b26\u96c6\u548c\u5b57\u7b26\u6392\u5e8f\u898f\u5247\u3002\u9019\u6a23\u53ef\u4ee5\u907f\u514d\u5728\u67e5\u8a62\u904e\u7a0b\u4e2d\u767c\u751f\u96b1\u5f0f\u8f49\u63db\u3002\u4f8b\u5982\uff0c\u5982\u679c\u4e00\u500b\u8868\u7684\u5b57\u7b26\u96c6\u662f UTF-8\uff0c\u5247\u6240\u6709\u76f8\u95dc\u7684\u67e5\u8a62\u548c\u6578\u64da\u63d2\u5165\u90fd\u61c9\u4f7f\u7528\u76f8\u540c\u7684\u5b57\u7b26\u96c6\u3002<\/p>\n<h3 id=\"2-%e4%bd%bf%e7%94%a8%e9%a1%af%e5%bc%8f%e8%bd%89%e6%8f%9b-aGsyZWJxOK\">2. \u4f7f\u7528\u986f\u5f0f\u8f49\u63db<\/h3>\n<p>\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u4f7f\u7528\u986f\u5f0f\u8f49\u63db\u53ef\u4ee5\u63d0\u9ad8\u67e5\u8a62\u7684\u53ef\u8b80\u6027\u548c\u6027\u80fd\u3002\u901a\u904e\u4f7f\u7528 SQL \u7684 CAST \u6216 CONVERT \u51fd\u6578\uff0c\u53ef\u4ee5\u660e\u78ba\u6307\u5b9a\u6240\u9700\u7684\u5b57\u7b26\u96c6\uff0c\u5f9e\u800c\u907f\u514d\u96b1\u5f0f\u8f49\u63db\u3002\u4f8b\u5982\uff1a<\/p>\n<pre><code>SELECT * FROM users WHERE username = CONVERT('example' USING utf8mb4);<\/code><\/pre>\n<h3 id=\"3-%e5%84%aa%e5%8c%96%e6%9f%a5%e8%a9%a2%e8%aa%9e%e5%8f%a5-aGsyZWJxOK\">3. \u512a\u5316\u67e5\u8a62\u8a9e\u53e5<\/h3>\n<p>\u5728\u64b0\u5beb SQL \u67e5\u8a62\u6642\uff0c\u61c9\u907f\u514d\u5728 WHERE \u5b50\u53e5\u4e2d\u4f7f\u7528\u4e0d\u540c\u5b57\u7b26\u96c6\u7684\u5217\u9032\u884c\u6bd4\u8f03\u3002\u9019\u6a23\u53ef\u4ee5\u6e1b\u5c11\u96b1\u5f0f\u8f49\u63db\u7684\u767c\u751f\u3002\u4f8b\u5982\uff0c\u61c9\u907f\u514d\u4ee5\u4e0b\u67e5\u8a62\uff1a<\/p>\n<pre><code>SELECT * FROM users WHERE username = 'example' COLLATE utf8mb4_general_ci;<\/code><\/pre>\n<p>\u76f8\u53cd\uff0c\u61c9\u78ba\u4fdd\u6240\u6709\u6bd4\u8f03\u7684\u5217\u90fd\u4f7f\u7528\u76f8\u540c\u7684\u5b57\u7b26\u96c6\u3002<\/p>\n<h2 id=\"%e7%b5%90%e8%ab%96-aGsyZWJxOK\">\u7d50\u8ad6<\/h2>\n<p>\u96b1\u5f0f\u5b57\u7b26\u7de8\u78bc\u8f49\u63db\u5728 SQL \u67e5\u8a62\u4e2d\u53ef\u80fd\u6703\u5c0e\u81f4\u6027\u80fd\u4e0b\u964d\u548c\u6578\u64da\u4e0d\u4e00\u81f4\u6027\u3002\u901a\u904e\u78ba\u4fdd\u5b57\u7b26\u96c6\u7684\u4e00\u81f4\u6027\u3001\u4f7f\u7528\u986f\u5f0f\u8f49\u63db\u4ee5\u53ca\u512a\u5316\u67e5\u8a62\u8a9e\u53e5\uff0c\u53ef\u4ee5\u6709\u6548\u6e1b\u5c11\u96b1\u5f0f\u8f49\u63db\u7684\u5f71\u97ff\uff0c\u5f9e\u800c\u63d0\u5347\u6578\u64da\u5eab\u7684\u6027\u80fd\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6548\u6578\u64da\u8655\u7406\u7684\u61c9\u7528\u7a0b\u5e8f\u4f86\u8aaa\uff0c\u9019\u4e9b\u512a\u5316\u63aa\u65bd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u3002<\/p>\n<p>\u5982\u9700\u4e86\u89e3\u66f4\u591a\u6709\u95dc <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u548c\u6578\u64da\u5eab\u512a\u5316\u7684\u8cc7\u8a0a\uff0c\u8acb\u8a2a\u554f\u6211\u5011\u7684\u7db2\u7ad9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22 SQL \u512a\u5316\u6280\u8853\uff0c\u4e86\u89e3\u96b1\u5f0f\u5b57\u7b26\u7de8\u78bc\u8f49\u63db\u5982\u4f55\u63d0\u5347\u67e5\u8a62\u6027\u80fd\uff0c\u6e1b\u5c11\u932f\u8aa4\uff0c\u78ba\u4fdd\u6578\u64da\u6e96\u78ba\u6027\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-168425","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/168425","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=168425"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/168425\/revisions"}],"predecessor-version":[{"id":168426,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/168425\/revisions\/168426"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=168425"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=168425"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=168425"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}