{"id":6795,"date":"2024-11-14T13:27:11","date_gmt":"2024-11-14T05:27:11","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6795\/"},"modified":"2024-11-14T13:27:11","modified_gmt":"2024-11-14T05:27:11","slug":"mysql-%e4%b8%ad%e9%81%87%e5%88%b0-collate-%e6%8a%a5%e9%94%99%ef%bc%9a%e5%a6%82%e4%bd%95%e5%bf%bd%e7%95%a5%e5%ad%97%e7%ac%a6%e9%9b%86%e5%b7%ae%e5%bc%82%e6%9f%a5%e8%af%a2%e7%9b%b8%e5%90%8c%e6%95%b0","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6795\/","title":{"rendered":"MySQL \u4e2d\u9047\u5230 COLLATE \u62a5\u9519\uff1a\u5982\u4f55\u5ffd\u7565\u5b57\u7b26\u96c6\u5dee\u5f02\u67e5\u8be2\u76f8\u540c\u6570\u636e\uff1f"},"content":{"rendered":"<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/001\/246\/273\/172993213435319.jpg\" class=\"aligncenter\" title=\"MySQL \u4e2d\u9047\u5230 COLLATE \u62a5\u9519\uff1a\u5982\u4f55\u5ffd\u7565\u5b57\u7b26\u96c6\u5dee\u5f02\u67e5\u8be2\u76f8\u540c\u6570\u636e\uff1f\u63d2\u56fe\" alt=\"MySQL \u4e2d\u9047\u5230 COLLATE \u62a5\u9519\uff1a\u5982\u4f55\u5ffd\u7565\u5b57\u7b26\u96c6\u5dee\u5f02\u67e5\u8be2\u76f8\u540c\u6570\u636e\uff1f\u63d2\u56fe\" \/><\/p>\n<p><strong> \u4e2d\u5904\u7406 collate \u62a5\u9519\u7684\u5de7\u5999\u65b9\u6cd5<\/strong><\/p>\n<p>\u5728 mysql \u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f7f\u7528 collate \u6765\u6307\u5b9a\u5b57\u7b26\u96c6\u548c\u6392\u5e8f\u89c4\u5219\u3002\u4f46\u662f\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u4f1a\u9047\u5230 collate \u62a5\u9519\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u6709\u4e00\u4e2a\u8868\u4e2d\u5b58\u5728\u4e24\u4e2a\u5b57\u7b26\u96c6\u76f8\u540c\u7684\u5b57\u6bb5\uff1a<\/p>\n<pre>create table user (\n  id int not null primary key,\n  username varchar(255) unique\n) character set gbk;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff0cusername \u5b57\u6bb5\u7684\u503c &#8220;forever&#8221; \u548c &#8220;f\u00f2r\u0113v\u00e8r&#8221; \u5728 gbk \u5b57\u7b26\u96c6\u4e0b\u88ab\u89c6\u4e3a\u4e0d\u540c\u7684\u5b57\u7b26\uff0c\u4f46\u5728 utf8 \u5b57\u7b26\u96c6\u4e0b\u5374\u89c6\u4e3a\u76f8\u540c\u3002\u73b0\u5728\uff0c\u6211\u4eec\u60f3\u8981\u67e5\u8be2\u51fa\u6240\u6709\u5177\u6709\u76f8\u540c username\uff08\u5ffd\u7565\u5b57\u7b26\u96c6\u5dee\u5f02\uff09\u7684\u8bb0\u5f55\uff1a<\/p>\n<pre>select username from user group by username collate utf8mb4_general_ci;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7136\u800c\uff0c\u8fd9\u4e2a\u67e5\u8be2\u4f1a\u62a5\u9519\uff1a<\/p>\n<pre>#1366 - incorrect string value: '\ufffd\ufffd...' for column 'username' at row 1<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5982\u679c\u6211\u4eec\u65e0\u6cd5\u4f7f\u7528 collate\uff0c\u8be5\u5982\u4f55\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u5462\uff1f\u4e00\u79cd\u5de7\u5999\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 convert() \u51fd\u6570\uff0c\u5c06 username \u5b57\u6bb5\u5f3a\u5236\u8f6c\u6362\u4e3a utf8mb4 \u5b57\u7b26\u96c6\u5e76\u5bf9\u5176\u8fdb\u884c\u5206\u7ec4\uff1a<\/p>\n<pre>SELECT \n  convert(username, char character set utf8mb4) collate utf8mb4_general_ci\nFROM \n  user\nGROUP BY \n  convert(username, char character set utf8mb4) collate utf8mb4_general_ci;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u8fd9\u6837\u4e00\u6765\uff0c\u67e5\u8be2\u5c06\u6210\u529f\u6267\u884c\uff0c\u5e76\u8fd4\u56de\u6240\u6709\u5177\u6709\u76f8\u540c username\uff08\u65e0\u8bba\u5b57\u7b26\u96c6\u5dee\u5f02\u5982\u4f55\uff09\u7684\u8bb0\u5f55\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fMySQL \u4e2d\u9047\u5230 COLLATE \u62a5\u9519\uff1a\u5982\u4f55\u5ffd\u7565\u5b57\u7b26\u96c6\u5dee\u5f02\u67e5\u8be2\u76f8\u540c\u6570\u636e\uff1f\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>\u4e2d\u5904\u7406 collate \u62a5\u9519\u7684\u5de7\u5999\u65b9\u6cd5 \u5728 mysql \u4e2d\uff0c\u6211\u4eec\u7ecf\u5e38\u4f7f\u7528 collate \u6765\u6307\u5b9a\u5b57\u7b26\u96c6\u548c\u6392\u5e8f\u89c4\u5219\u3002\u4f46\u662f\uff0c\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u4f1a\u9047\u5230 collate \u62a5\u9519\u3002\u4e3e\u4e2a\u4f8b\u5b50\uff0c\u6709\u4e00\u4e2a\u8868\u4e2d\u5b58\u5728\u4e24\u4e2a\u5b57\u7b26\u96c6\u76f8\u540c\u7684\u5b57\u6bb5\uff1a create table user ( id int not null primary key, username varchar(255) unique ) character set gbk; \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff0cusername \u5b57\u6bb5\u7684\u503c &#8220;forever&#8221; \u548c &#8220;f\u00f2r\u0113v\u00e8r&#8221; \u5728 gbk \u5b57\u7b26\u96c6\u4e0b\u88ab\u89c6\u4e3a\u4e0d\u540c\u7684\u5b57\u7b26\uff0c\u4f46\u5728 utf8 \u5b57\u7b26\u96c6\u4e0b\u5374\u89c6\u4e3a\u76f8\u540c\u3002\u73b0\u5728\uff0c\u6211\u4eec\u60f3\u8981\u67e5\u8be2\u51fa\u6240\u6709\u5177\u6709\u76f8\u540c username\uff08\u5ffd\u7565\u5b57\u7b26\u96c6\u5dee\u5f02\uff09\u7684\u8bb0\u5f55\uff1a select username from user group by username collate utf8mb4_general_ci; \u767b\u5f55\u540e\u590d\u5236 \u7136\u800c\uff0c\u8fd9\u4e2a\u67e5\u8be2\u4f1a\u62a5\u9519\uff1a #1366 &#8211; incorrect string value: [&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-6795","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6795","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=6795"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6795\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6795"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6795"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6795"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}