{"id":5261,"date":"2024-11-14T10:22:20","date_gmt":"2024-11-14T02:22:20","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5261\/"},"modified":"2024-11-14T10:22:20","modified_gmt":"2024-11-14T02:22:20","slug":"mysql%e6%95%b0%e6%8d%ae%e5%ba%93%e4%b9%b1%e7%a0%81%e6%80%8e%e4%b9%88%e8%a7%a3%e5%86%b3","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5261\/","title":{"rendered":"mysql\u6570\u636e\u5e93\u4e71\u7801\u600e\u4e48\u89e3\u51b3"},"content":{"rendered":"<blockquote><p>\n  mysql \u6570\u636e\u5e93\u51fa\u73b0\u4e71\u7801\u89e3\u51b3\u65b9\u6cd5\uff1a\u68c0\u67e5\u5b57\u7b26\u96c6\u548c\u6821\u5bf9\u89c4\u5219\u662f\u5426\u6b63\u786e\u3002\u4f7f\u7528 unicode \u5b57\u7b26\u96c6\uff0c\u5982 utf-8 \u6216 utf-16\u3002\u8f6c\u6362\u73b0\u6709\u4e71\u7801\u6570\u636e\uff0c\u4f7f\u7528 convert() \u51fd\u6570\u3002\u4fee\u6539\u5ba2\u6237\u7aef\u8bbe\u7f6e\uff0c\u786e\u4fdd\u4e0e\u6570\u636e\u5e93\u4e00\u81f4\u3002\u786e\u4fdd\u8868\u7684\u8868\u5934\u5b9a\u4e49\u4e86\u6b63\u786e\u7684\u5b57\u7b26\u96c6\u548c\u6821\u5bf9\u89c4\u5219\u3002\u68c0\u67e5\u8fde\u63a5\u8bbe\u7f6e\u4e2d\u662f\u5426\u6307\u5b9a\u4e86\u6b63\u786e\u7684\u5b57\u7b26\u96c6\u548c\u6821\u5bf9\u89c4\u5219\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/26\/2024052617542459643.jpg\" class=\"aligncenter\" title=\"mysql\u6570\u636e\u5e93\u4e71\u7801\u600e\u4e48\u89e3\u51b3\u63d2\u56fe\" alt=\"mysql\u6570\u636e\u5e93\u4e71\u7801\u600e\u4e48\u89e3\u51b3\u63d2\u56fe\" \/><\/p>\n<p><strong>MySQL \u6570\u636e\u5e93\u4e71\u7801\u89e3\u51b3\u65b9\u6cd5<\/strong><\/p>\n<p><strong>\u95ee\u9898\uff1a<\/strong> MySQL \u6570\u636e\u5e93\u4e2d\u51fa\u73b0\u4e71\u7801\u73b0\u8c61\u8be5\u5982\u4f55\u89e3\u51b3\uff1f<\/p>\n<p><strong>\u89e3\u51b3\u65b9\u6848\uff1a<\/strong><\/p>\n<p><strong>1. \u68c0\u67e5\u5b57\u7b26\u96c6\u548c\u6821\u5bf9\u89c4\u5219<\/strong><\/p>\n<ul>\n<li>\u786e\u4fdd\u6570\u636e\u5e93\u3001\u8868\u548c\u5217\u7684\u5b57\u7b26\u96c6\u548c\u6821\u5bf9\u89c4\u5219\u8bbe\u7f6e\u6b63\u786e\u3002<\/li>\n<li>\u4f7f\u7528 SHOW CREATE DATABASE\/TABLE\/COLUMN \u547d\u4ee4\u6765\u67e5\u770b\u5f53\u524d\u8bbe\u7f6e\u3002<\/li>\n<li>\u5982\u6709\u95ee\u9898\uff0c\u53ef\u4f7f\u7528 ALTER DATABASE\/TABLE\/COLUMN \u547d\u4ee4\u6765\u4fee\u6539\u8bbe\u7f6e\u3002<\/li>\n<\/ul>\n<p><strong>2. \u4f7f\u7528 Unicode \u5b57\u7b26\u96c6<\/strong><\/p>\n<ul>\n<li>Unicode \u5b57\u7b26\u96c6\u652f\u6301\u5e7f\u6cdb\u7684\u8bed\u8a00\u548c\u5b57\u7b26\u96c6\uff0c\u53ef\u907f\u514d\u4e71\u7801\u3002<\/li>\n<li>\u4f7f\u7528 UTF-8 \u6216 UTF-16 \u5b57\u7b26\u96c6\uff0c\u5e76\u8bbe\u7f6e\u76f8\u5e94\u7684\u6821\u5bf9\u89c4\u5219\uff0c\u5982 utf8_general_ci\u3002<\/li>\n<\/ul>\n<p><strong>3. \u8f6c\u6362\u73b0\u6709\u6570\u636e<\/strong><\/p>\n<ul>\n<li>\u5982\u679c\u73b0\u6709\u6570\u636e\u5df2\u51fa\u73b0\u4e71\u7801\uff0c\u53ef\u4f7f\u7528 CONVERT() \u51fd\u6570\u6765\u8f6c\u6362\u5b57\u7b26\u96c6\u3002<\/li>\n<li>\u4f8b\u5982\uff1aUPDATE table SET column = CONVERT(column USING utf8);<\/li>\n<\/ul>\n<p><strong>4. \u4fee\u6539\u5ba2\u6237\u7aef\u8bbe\u7f6e<\/strong><\/p>\n<ul>\n<li>\u786e\u4fdd\u5ba2\u6237\u7aef\u7a0b\u5e8f\u7684\u5b57\u7b26\u96c6\u548c\u6821\u5bf9\u89c4\u5219\u4e0e\u6570\u636e\u5e93\u8bbe\u7f6e\u4e00\u81f4\u3002<\/li>\n<li>\u5728\u8fde\u63a5\u6570\u636e\u5e93\u65f6\uff0c\u6307\u5b9a\u5b57\u7b26\u96c6\u548c\u6821\u5bf9\u89c4\u5219\u3002<\/li>\n<li>\u4f8b\u5982\uff0c\u5bf9\u4e8e Python \u4f7f\u7528 charset=&#8221;utf8&#8243;\u3002<\/li>\n<\/ul>\n<p><strong>5. \u786e\u4fdd\u8868\u7684\u8868\u5934\u5b9a\u4e49<\/strong><\/p>\n<ul>\n<li>\u786e\u4fdd\u8868\u7684\u8868\u5934\u6b63\u786e\u5b9a\u4e49\u4e86\u5b57\u7b26\u96c6\u548c\u6821\u5bf9\u89c4\u5219\u3002<\/li>\n<li>\u5728\u521b\u5efa\u8868\u65f6\u4f7f\u7528 CHARACTER SET \u548c COLLATE \u5b50\u53e5\u6307\u5b9a\u8bbe\u7f6e\u3002<\/li>\n<li>\u4f8b\u5982\uff1aCREATE TABLE table_name (column_name VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci);<\/li>\n<\/ul>\n<p><strong>6. \u68c0\u67e5\u8fde\u63a5\u8bbe\u7f6e<\/strong><\/p>\n<ul>\n<li>\u786e\u8ba4\u6570\u636e\u5e93\u8fde\u63a5\u5b57\u7b26\u4e32\u4e2d\u6307\u5b9a\u4e86\u6b63\u786e\u7684\u5b57\u7b26\u96c6\u548c\u6821\u5bf9\u89c4\u5219\u3002<\/li>\n<li>\u5bf9\u4e8e PHP\uff0c\u4f7f\u7528 init_connect() \u51fd\u6570\u6765\u8bbe\u7f6e\u5b57\u7b26\u96c6\u3002<\/li>\n<li>\u4f8b\u5982\uff1ainit_connect(&#8220;SET NAMES &#8216;utf8&#8217;;&#8221;);<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u6570\u636e\u5e93\u4e71\u7801\u600e\u4e48\u89e3\u51b3\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>mysql \u6570\u636e\u5e93\u51fa\u73b0\u4e71\u7801\u89e3\u51b3\u65b9\u6cd5\uff1a\u68c0\u67e5\u5b57\u7b26\u96c6\u548c\u6821\u5bf9\u89c4\u5219\u662f\u5426\u6b63\u786e\u3002\u4f7f\u7528 unicode \u5b57\u7b26\u96c6\uff0c\u5982 utf-8 \u6216 utf-16\u3002\u8f6c\u6362\u73b0\u6709\u4e71\u7801\u6570\u636e\uff0c\u4f7f\u7528 convert() \u51fd\u6570\u3002\u4fee\u6539\u5ba2\u6237\u7aef\u8bbe\u7f6e\uff0c\u786e\u4fdd\u4e0e\u6570\u636e\u5e93\u4e00\u81f4\u3002\u786e\u4fdd\u8868\u7684\u8868\u5934\u5b9a\u4e49\u4e86\u6b63\u786e\u7684\u5b57\u7b26\u96c6\u548c\u6821\u5bf9\u89c4\u5219\u3002\u68c0\u67e5\u8fde\u63a5\u8bbe\u7f6e\u4e2d\u662f\u5426\u6307\u5b9a\u4e86\u6b63\u786e\u7684\u5b57\u7b26\u96c6\u548c\u6821\u5bf9\u89c4\u5219\u3002 MySQL \u6570\u636e\u5e93\u4e71\u7801\u89e3\u51b3\u65b9\u6cd5 \u95ee\u9898\uff1a MySQL \u6570\u636e\u5e93\u4e2d\u51fa\u73b0\u4e71\u7801\u73b0\u8c61\u8be5\u5982\u4f55\u89e3\u51b3\uff1f \u89e3\u51b3\u65b9\u6848\uff1a 1. \u68c0\u67e5\u5b57\u7b26\u96c6\u548c\u6821\u5bf9\u89c4\u5219 \u786e\u4fdd\u6570\u636e\u5e93\u3001\u8868\u548c\u5217\u7684\u5b57\u7b26\u96c6\u548c\u6821\u5bf9\u89c4\u5219\u8bbe\u7f6e\u6b63\u786e\u3002 \u4f7f\u7528 SHOW CREATE DATABASE\/TABLE\/COLUMN \u547d\u4ee4\u6765\u67e5\u770b\u5f53\u524d\u8bbe\u7f6e\u3002 \u5982\u6709\u95ee\u9898\uff0c\u53ef\u4f7f\u7528 ALTER DATABASE\/TABLE\/COLUMN \u547d\u4ee4\u6765\u4fee\u6539\u8bbe\u7f6e\u3002 2. \u4f7f\u7528 Unicode \u5b57\u7b26\u96c6 Unicode \u5b57\u7b26\u96c6\u652f\u6301\u5e7f\u6cdb\u7684\u8bed\u8a00\u548c\u5b57\u7b26\u96c6\uff0c\u53ef\u907f\u514d\u4e71\u7801\u3002 \u4f7f\u7528 UTF-8 \u6216 UTF-16 \u5b57\u7b26\u96c6\uff0c\u5e76\u8bbe\u7f6e\u76f8\u5e94\u7684\u6821\u5bf9\u89c4\u5219\uff0c\u5982 utf8_general_ci\u3002 3. \u8f6c\u6362\u73b0\u6709\u6570\u636e \u5982\u679c\u73b0\u6709\u6570\u636e\u5df2\u51fa\u73b0\u4e71\u7801\uff0c\u53ef\u4f7f\u7528 CONVERT() \u51fd\u6570\u6765\u8f6c\u6362\u5b57\u7b26\u96c6\u3002 \u4f8b\u5982\uff1aUPDATE table SET column = CONVERT(column USING utf8); 4. \u4fee\u6539\u5ba2\u6237\u7aef\u8bbe\u7f6e \u786e\u4fdd\u5ba2\u6237\u7aef\u7a0b\u5e8f\u7684\u5b57\u7b26\u96c6\u548c\u6821\u5bf9\u89c4\u5219\u4e0e\u6570\u636e\u5e93\u8bbe\u7f6e\u4e00\u81f4\u3002 [&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-5261","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5261","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=5261"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5261\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5261"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5261"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5261"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}