{"id":201008,"date":"2025-05-06T12:40:26","date_gmt":"2025-05-06T04:40:26","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201008\/"},"modified":"2025-05-06T12:40:26","modified_gmt":"2025-05-06T04:40:26","slug":"mysql-%e8%a1%a8%e5%ad%97%e7%ac%a6%e9%9b%86%e4%b8%8d%e5%90%8c%ef%bc%8c%e5%a6%82%e4%bd%95%e6%89%be%e5%87%ba%e5%ad%97%e7%ac%a6%e5%86%85%e5%ae%b9%e7%9b%b8%e5%90%8c%e7%9a%84%e8%ae%b0%e5%bd%95%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201008\/","title":{"rendered":"MySQL \u8868\u5b57\u7b26\u96c6\u4e0d\u540c\uff0c\u5982\u4f55\u627e\u51fa\u5b57\u7b26\u5185\u5bb9\u76f8\u540c\u7684\u8bb0\u5f55\uff1f"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>MySQL \u8868\u5b57\u7b26\u96c6\u4e0d\u540c\uff0c\u5982\u4f55\u627e\u51fa\u5b57\u7b26\u5185\u5bb9\u76f8\u540c\u7684\u8bb0\u5f55\uff1f<\/h1>\n<p>\u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300aMySQL \u8868\u5b57\u7b26\u96c6\u4e0d\u540c\uff0c\u5982\u4f55\u627e\u51fa\u5b57\u7b26\u5185\u5bb9\u76f8\u540c\u7684\u8bb0\u5f55\uff1f\u300b\uff0c\u4e3b\u8981\u5305\u62ec\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241125\/1732511737674407f9ed88d.jpg\" class=\"aligncenter\"><\/p>\n<p><strong>mysql \u8868\u4e2d\u5b57\u7b26\u96c6\u5904\u7406\u9047\u5230\u7684 collate \u76f8\u5173\u95ee\u9898<\/strong><\/p>\n<p><strong>\u95ee\u9898\u63d0\u51fa\uff1a<\/strong><\/p>\n<p>\u5728 mysql \u8868\u4e2d\uff0c\u4f7f\u7528 collate \u65f6\u9047\u5230\u62a5\u9519\uff0c\u65e0\u6cd5\u6309\u9884\u671f\u627e\u51fa\u5b57\u7b26\u96c6\u4e0d\u540c\u4f46\u5b57\u7b26\u5185\u5bb9\u76f8\u540c\u7684\u8bb0\u5f55\u3002<\/p>\n<p><strong>\u95ee\u9898\u63cf\u8ff0\uff1a<\/strong><\/p>\n<ul>\n<li>\u8868\u5b57\u7b26\u96c6\u4e3agbk\uff0cusername \u5b57\u6bb5\u4e3a\u552f\u4e00\u952e\u3002<\/li>\n<li>\u90e8\u5206\u8bb0\u5f55\u7684 username \u503c\u5728 gbk \u5b57\u7b26\u96c6\u4e0b\u88ab\u89c6\u4e3a\u4e0d\u540c\u5b57\u7b26\uff0c\u4f46\u5728 utf-8 \u5b57\u7b26\u96c6\u4e0b\u88ab\u89c6\u4e3a\u540c\u4e00\u5b57\u7b26\u3002<\/li>\n<li>\u4f7f\u7528 select username from test.user group by username collate utf8mb4_general_ci; \u8bed\u53e5\u67e5\u8be2\u7ed3\u679c\u62a5\u9519\u3002<\/li>\n<\/ul>\n<p><strong>\u89e3\u51b3\u65b9\u6cd5\uff1a<\/strong><\/p>\n<p>\u8981\u89e3\u51b3\u6b64\u95ee\u9898\uff0c\u6709\u4ee5\u4e0b\u89e3\u51b3\u529e\u6cd5\uff1a<\/p>\n<p>\u4e0d\u4f7f\u7528 collate \u7684\u66ff\u4ee3\u65b9\u6cd5\uff1a<\/p>\n<p><strong>1. \u4f7f\u7528 convert() \u8f6c\u6362\u5b57\u7b26\u96c6\uff1a<\/strong><\/p>\n<pre>select convert(username, char character set utf8mb4) collate utf8mb4_general_ci\nfrom test.user\ngroup by convert(username, char character set utf8mb4) collate utf8mb4_general_ci;<\/pre>\n<p>\u6b64\u8bed\u53e5\u5c06\u628a username \u5b57\u6bb5\u8f6c\u6362\u4e3a utf-8 \u5b57\u7b26\u96c6\uff0c\u5e76\u6309\u8f6c\u6362\u540e\u7684\u503c\u8fdb\u884c\u5206\u7ec4\u3002<\/p>\n<p><strong>2. \u4f7f\u7528 binary \u5ffd\u7565\u5b57\u7b26\u96c6\u5dee\u5f02\uff1a<\/strong><\/p>\n<pre>select username\nfrom test.user\ngroup by binary username;<\/pre>\n<p>binary \u5173\u952e\u5b57\u5c06\u6309\u4e8c\u8fdb\u5236\u503c\u5bf9\u5b57\u7b26\u8fdb\u884c\u5206\u7ec4\uff0c\u5ffd\u7565\u5b57\u7b26\u96c6\u5dee\u5f02\u3002<\/p>\n<p><strong>\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u4fee\u6539\u8868\u7ed3\u6784\uff1a<\/strong><\/p>\n<p><strong>1. \u4fee\u6539\u8868\u5b57\u7b26\u96c6\u4e3a utf-8\uff1a<\/strong><\/p>\n<pre>alter table test.user convert to character set utf8mb4;<\/pre>\n<p><strong>2. \u5728\u5b57\u6bb5\u5b9a\u4e49\u4e2d\u6307\u5b9a collate\uff1a<\/strong><\/p>\n<pre>CREATE TABLE test.user (\n  username VARCHAR(255) COLLATE utf8mb4_general_ci NOT NULL,\n  ...\n);<\/pre>\n<p>\u901a\u8fc7\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6309\u671f\u671b\u627e\u51fa\u5b57\u7b26\u96c6\u4e0d\u540c\u4f46\u5b57\u7b26\u5185\u5bb9\u76f8\u540c\u7684\u8bb0\u5f55\u3002<\/p>\n<p>\u4eca\u5929\u5e26\u5927\u5bb6\u4e86\u89e3\u4e86\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u5e0c\u671b\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\uff1b\u5173\u4e8e\u6570\u636e\u5e93\u7684\u6280\u672f\u77e5\u8bc6\u6211\u4eec\u4f1a\u4e00\u70b9\u70b9\u6df1\u5165\u4ecb\u7ecd\uff0c\u6b22\u8fce\u5927\u5bb6\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\uff0c\u4e00\u8d77\u5b66\u4e60\u7f16\u7a0b~<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>MySQL \u8868\u5b57\u7b26\u96c6\u4e0d\u540c\uff0c\u5982\u4f55\u627e&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-201008","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201008","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"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=201008"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201008\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201008"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201008"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201008"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}