{"id":80826,"date":"2024-10-16T14:45:14","date_gmt":"2024-10-16T06:45:14","guid":{"rendered":"https:\/\/server.hk\/cnblog\/80826\/"},"modified":"2024-10-16T14:45:14","modified_gmt":"2024-10-16T06:45:14","slug":"mysql%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%a4%a7%e5%b0%8f%e5%af%ab%e5%95%8f%e9%a1%8c%e7%9a%845%e7%a8%ae%e8%a7%a3%e6%b1%ba%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/80826\/","title":{"rendered":"MySQL\u5b57\u7b26\u4e32\u5927\u5c0f\u5beb\u554f\u984c\u76845\u7a2e\u89e3\u6c7a\u65b9\u6cd5"},"content":{"rendered":"<h1 id=\"mysql%e5%ad%97%e7%ac%a6%e4%b8%b2%e5%a4%a7%e5%b0%8f%e5%af%ab%e5%95%8f%e9%a1%8c%e7%9a%845%e7%a8%ae%e8%a7%a3%e6%b1%ba%e6%96%b9%e6%b3%95-mVfmxGjFZJ\">MySQL\u5b57\u7b26\u4e32\u5927\u5c0f\u5beb\u554f\u984c\u76845\u7a2e\u89e3\u6c7a\u65b9\u6cd5<\/h1>\n<p>\u5728\u4f7f\u7528MySQL\u9032\u884c\u6578\u64da\u5eab\u7ba1\u7406\u6642\uff0c\u5b57\u7b26\u4e32\u7684\u5927\u5c0f\u5beb\u554f\u984c\u5e38\u5e38\u6703\u5f15\u767c\u4e00\u4e9b\u56f0\u64fe\u3002\u9019\u4e0d\u50c5\u5f71\u97ff\u67e5\u8a62\u7684\u7d50\u679c\uff0c\u9084\u53ef\u80fd\u5c0e\u81f4\u6578\u64da\u4e0d\u4e00\u81f4\u6027\u3002\u672c\u6587\u5c07\u63a2\u8a0e\u4e94\u7a2e\u89e3\u6c7aMySQL\u5b57\u7b26\u4e32\u5927\u5c0f\u5beb\u554f\u984c\u7684\u65b9\u6cd5\uff0c\u5e6b\u52a9\u958b\u767c\u8005\u66f4\u6709\u6548\u5730\u7ba1\u7406\u6578\u64da\u3002<\/p>\n<h2 id=\"1-%e4%bd%bf%e7%94%a8binary%e9%97%9c%e9%8d%b5%e5%ad%97-mVfmxGjFZJ\">1. \u4f7f\u7528BINARY\u95dc\u9375\u5b57<\/h2>\n<p>\u5728MySQL\u4e2d\uff0c\u4f7f\u7528\u95dc\u9375\u5b57\u53ef\u4ee5\u5f37\u5236\u9032\u884c\u5927\u5c0f\u5beb\u654f\u611f\u7684\u6bd4\u8f03\u3002\u9019\u610f\u5473\u8457\u5728\u67e5\u8a62\u6642\uff0c\u5b57\u7b26\u4e32\u7684\u5927\u5c0f\u5beb\u5c07\u88ab\u8996\u70ba\u4e0d\u540c\u7684\u5b57\u7b26\u3002<\/p>\n<pre><code>SELECT * FROM users WHERE BINARY username = 'Admin';\n<\/code><\/pre>\n<p>\u9019\u6a23\u7684\u67e5\u8a62\u5c07\u53ea\u8fd4\u56de\u7528\u6236\u540d\u70ba\u201cAdmin\u201d\u7684\u8a18\u9304\uff0c\u800c\u4e0d\u6703\u8fd4\u56de\u201cadmin\u201d\u6216\u201cADMIN\u201d\u3002<\/p>\n<h2 id=\"2-%e4%bf%ae%e6%94%b9%e5%ad%97%e7%ac%a6%e9%9b%86%e5%92%8c%e6%8e%92%e5%ba%8f%e8%a6%8f%e5%89%87-mVfmxGjFZJ\">2. \u4fee\u6539\u5b57\u7b26\u96c6\u548c\u6392\u5e8f\u898f\u5247<\/h2>\n<p>MySQL\u7684\u5b57\u7b26\u96c6\u548c\u6392\u5e8f\u898f\u5247\uff08collation\uff09\u6703\u5f71\u97ff\u5b57\u7b26\u4e32\u7684\u6bd4\u8f03\u65b9\u5f0f\u3002\u53ef\u4ee5\u901a\u904e\u8a2d\u7f6e\u5b57\u7b26\u96c6\u70ba\u5927\u5c0f\u5beb\u654f\u611f\u7684\u9078\u9805\u4f86\u89e3\u6c7a\u554f\u984c\u3002\u4f8b\u5982\uff0c\u4f7f\u7528utf8_bin\u5b57\u7b26\u96c6\u53ef\u4ee5\u5be6\u73fe\u5927\u5c0f\u5beb\u654f\u611f\u7684\u6bd4\u8f03\u3002<\/p>\n<pre><code>ALTER TABLE users CONVERT TO CHARACTER SET utf8 COLLATE utf8_bin;\n<\/code><\/pre>\n<p>\u9019\u6a23\u8a2d\u7f6e\u5f8c\uff0c\u6240\u6709\u7684\u5b57\u7b26\u4e32\u6bd4\u8f03\u90fd\u5c07\u662f\u5927\u5c0f\u5beb\u654f\u611f\u7684\u3002<\/p>\n<h2 id=\"3-%e4%bd%bf%e7%94%a8lower%e6%88%96upper%e5%87%bd%e6%95%b8-mVfmxGjFZJ\">3. \u4f7f\u7528LOWER()\u6216UPPER()\u51fd\u6578<\/h2>\n<p>\u5728\u67e5\u8a62\u4e2d\u4f7f\u7528LOWER()\u6216UPPER()\u51fd\u6578\u53ef\u4ee5\u5c07\u5b57\u7b26\u4e32\u8f49\u63db\u70ba\u7d71\u4e00\u7684\u5927\u5c0f\u5beb\uff0c\u5f9e\u800c\u907f\u514d\u5927\u5c0f\u5beb\u554f\u984c\u3002<\/p>\n<pre><code>SELECT * FROM users WHERE LOWER(username) = LOWER('Admin');\n<\/code><\/pre>\n<p>\u9019\u6a23\u7684\u67e5\u8a62\u5c07\u4e0d\u8ad6\u7528\u6236\u540d\u7684\u5927\u5c0f\u5beb\u5982\u4f55\uff0c\u90fd\u80fd\u6b63\u78ba\u8fd4\u56de\u7d50\u679c\u3002<\/p>\n<h2 id=\"4-%e5%9c%a8%e6%87%89%e7%94%a8%e5%b1%a4%e9%80%b2%e8%a1%8c%e8%99%95%e7%90%86-mVfmxGjFZJ\">4. \u5728\u61c9\u7528\u5c64\u9032\u884c\u8655\u7406<\/h2>\n<p>\u5728\u67d0\u4e9b\u60c5\u6cc1\u4e0b\uff0c\u53ef\u4ee5\u5728\u61c9\u7528\u5c64\u9032\u884c\u5b57\u7b26\u4e32\u7684\u5927\u5c0f\u5beb\u8655\u7406\u3002\u4f8b\u5982\uff0c\u5728\u63d2\u5165\u6578\u64da\u4e4b\u524d\uff0c\u5c07\u6240\u6709\u5b57\u7b26\u4e32\u8f49\u63db\u70ba\u5c0f\u5beb\u6216\u5927\u5beb\u3002\u9019\u6a23\u53ef\u4ee5\u78ba\u4fdd\u6578\u64da\u7684\u4e00\u81f4\u6027\u3002<\/p>\n<pre><code>username = input(\"Enter username: \").lower()\n<\/code><\/pre>\n<p>\u9019\u6a23\uff0c\u7121\u8ad6\u7528\u6236\u8f38\u5165\u4ec0\u9ebc\u6a23\u7684\u5927\u5c0f\u5beb\uff0c\u6700\u7d42\u5b58\u5165\u6578\u64da\u5eab\u7684\u90fd\u662f\u5c0f\u5beb\u5f62\u5f0f\u3002<\/p>\n<h2 id=\"5-%e4%bd%bf%e7%94%a8%e6%ad%a3%e5%89%87%e8%a1%a8%e9%81%94%e5%bc%8f-mVfmxGjFZJ\">5. \u4f7f\u7528\u6b63\u5247\u8868\u9054\u5f0f<\/h2>\n<p>MySQL\u652f\u6301\u6b63\u5247\u8868\u9054\u5f0f\uff0c\u53ef\u4ee5\u7528\u4f86\u9032\u884c\u66f4\u9748\u6d3b\u7684\u5b57\u7b26\u4e32\u5339\u914d\u3002\u4f7f\u7528REGEXP\u53ef\u4ee5\u5be6\u73fe\u5927\u5c0f\u5beb\u4e0d\u654f\u611f\u7684\u67e5\u8a62\u3002<\/p>\n<pre><code>SELECT * FROM users WHERE username REGEXP 'admin';\n<\/code><\/pre>\n<p>\u9019\u6a23\u7684\u67e5\u8a62\u5c07\u8fd4\u56de\u6240\u6709\u7528\u6236\u540d\u4e2d\u5305\u542b\u201cadmin\u201d\u7684\u8a18\u9304\uff0c\u4e0d\u8ad6\u5176\u5927\u5c0f\u5beb\u3002<\/p>\n<h2 id=\"%e7%b8%bd%e7%b5%90-mVfmxGjFZJ\">\u7e3d\u7d50<\/h2>\n<p>\u5728MySQL\u4e2d\u8655\u7406\u5b57\u7b26\u4e32\u5927\u5c0f\u5beb\u554f\u984c\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5f9e\u4f7f\u7528BINARY\u95dc\u9375\u5b57\u5230\u4fee\u6539\u5b57\u7b26\u96c6\uff0c\u751a\u81f3\u5728\u61c9\u7528\u5c64\u9032\u884c\u8655\u7406\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u6839\u64da\u5177\u9ad4\u9700\u6c42\u9078\u64c7\u5408\u9069\u7684\u89e3\u6c7a\u65b9\u6848\u3002\u4e86\u89e3\u9019\u4e9b\u65b9\u6cd5\u4e0d\u50c5\u80fd\u63d0\u9ad8\u6578\u64da\u67e5\u8a62\u7684\u6e96\u78ba\u6027\uff0c\u9084\u80fd\u589e\u5f37\u6578\u64da\u7684\u4e00\u81f4\u6027\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u7684\u5ba2\u6236\u3002\u7121\u8ad6\u662f <a href=\"https:\/\/server.hk\">\u9999\u6e2f\u4f3a\u670d\u5668<\/a> \u9084\u662f <a href=\"https:\/\/server.hk\">\u96f2\u4f3a\u670d\u5668<\/a>\uff0c\u6211\u5011\u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u9ad8\u6548\u7684\u670d\u52d9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22\u89e3\u6c7aMySQL\u5b57\u7b26\u4e32\u5927\u5c0f\u5beb\u554f\u984c\u76845\u7a2e\u6709\u6548\u65b9\u6cd5\uff0c\u63d0\u5347\u6578\u64da\u67e5\u8a62\u7684\u6e96\u78ba\u6027\u8207\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-80826","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/80826","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=80826"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/80826\/revisions"}],"predecessor-version":[{"id":80827,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/80826\/revisions\/80827"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=80826"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=80826"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=80826"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}