{"id":6214,"date":"2024-11-14T08:27:26","date_gmt":"2024-11-14T00:27:26","guid":{"rendered":"https:\/\/fwq.ai\/blog\/6214\/"},"modified":"2024-11-14T08:27:26","modified_gmt":"2024-11-14T00:27:26","slug":"mysql%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b%e5%a6%82%e4%bd%95%e8%bd%ac%e6%8d%a2","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/6214\/","title":{"rendered":"mysql\u6570\u636e\u7c7b\u578b\u5982\u4f55\u8f6c\u6362"},"content":{"rendered":"<blockquote><p>\n  \u5728 mysql \u4e2d\u8f6c\u6362\u6570\u636e\u7c7b\u578b\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528 alter table \u8bed\u53e5\u6307\u5b9a\u8981\u66f4\u6539\u7c7b\u578b\u7684\u8868\u548c\u5217\uff1b\u4f7f\u7528 cast() \u51fd\u6570\u4e34\u65f6\u5c06\u8868\u8fbe\u5f0f\u7684\u503c\u8f6c\u6362\u4e3a\u6307\u5b9a\u7684\u7c7b\u578b\uff1b\u521b\u5efa\u5177\u6709\u6240\u9700\u6570\u636e\u7c7b\u578b\u7684\u65b0\u8868\u5e76\u4ece\u65e7\u8868\u4e2d\u9009\u62e9\u6570\u636e\uff1b\u5bfc\u51fa\u6570\u636e\u5e76\u4f7f\u7528\u5bfc\u5165\u5de5\u5177\u5c06\u5176\u5bfc\u5165\u56de\u5177\u6709\u6240\u9700\u6570\u636e\u7c7b\u578b\u7684\u6570\u636e\u5e93\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202406\/15\/2024061513512761209.jpg\" class=\"aligncenter\" title=\"mysql\u6570\u636e\u7c7b\u578b\u5982\u4f55\u8f6c\u6362\u63d2\u56fe\" alt=\"mysql\u6570\u636e\u7c7b\u578b\u5982\u4f55\u8f6c\u6362\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u8f6c\u6362 MySQL \u6570\u636e\u7c7b\u578b<\/strong><\/p>\n<p>MySQL \u4e2d\u7684\u6570\u636e\u7c7b\u578b\u51b3\u5b9a\u4e86\u6570\u636e\u5e93\u4e2d\u5b58\u50a8\u6570\u636e\u7684\u683c\u5f0f\u548c\u9650\u5236\u3002\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u8f6c\u6362\u6570\u636e\u7684\u7c7b\u578b\u4ee5\u6ee1\u8db3\u7279\u5b9a\u7684\u9700\u6c42\u6216\u89e3\u51b3\u517c\u5bb9\u6027\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5728 MySQL \u4e2d\u8f6c\u6362\u6570\u636e\u7c7b\u578b\u7684\u6b65\u9aa4\uff1a<\/p>\n<p><strong>ALTER TABLE \u8bed\u53e5<\/strong><\/p>\n<p>\u9996\u5148\uff0c\u9700\u8981\u4f7f\u7528 ALTER TABLE \u8bed\u53e5\u6307\u5b9a\u8981\u66f4\u6539\u7c7b\u578b\u7684\u8868\u548c\u5217\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>ALTER TABLE table_name \nMODIFY column_name new_data_type;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4f8b\u5982\uff0c\u5c06 users \u8868\u4e2d age \u5217\u7684\u6570\u636e\u7c7b\u578b\u4ece INT \u8f6c\u6362\u4e3a VARCHAR(255)\uff1a<\/p>\n<pre>ALTER TABLE users \nMODIFY age VARCHAR(255);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>CAST() \u51fd\u6570<\/strong><\/p>\n<p>\u53e6\u4e00\u79cd\u8f6c\u6362\u6570\u636e\u7c7b\u578b\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 CAST() \u51fd\u6570\u3002\u6b64\u51fd\u6570\u53ef\u4ee5\u4e34\u65f6\u5c06\u8868\u8fbe\u5f0f\u7684\u503c\u8f6c\u6362\u4e3a\u6307\u5b9a\u7684\u7c7b\u578b\uff0c\u800c\u65e0\u9700\u6c38\u4e45\u66f4\u6539\u8868\u7ed3\u6784\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>CAST(expression AS new_data_type);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4f8b\u5982\uff0c\u5c06 orders \u8868\u4e2d total_price \u5217\u7684\u503c\u8f6c\u6362\u4e3a\u6d6e\u70b9\u578b\uff1a<\/p>\n<pre>SELECT CAST(total_price AS FLOAT) \nFROM orders;<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5176\u4ed6\u65b9\u5f0f<\/strong><\/p>\n<p>\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u65b9\u6cd5\u8f6c\u6362\u6570\u636e\u7c7b\u578b\uff1a<\/p>\n<ul>\n<li> <strong>CREATE TABLE&#8230;SELECT<\/strong>\uff1a\u521b\u5efa\u4e00\u4e2a\u5177\u6709\u6240\u9700\u6570\u636e\u7c7b\u578b\u7684\u65b0\u8868\uff0c\u5e76\u5c06\u6570\u636e\u4ece\u65e7\u8868\u4e2d\u9009\u62e9\u51fa\u6765\u3002<\/li>\n<li> <strong>\u6570\u636e\u5bfc\u51fa\/\u5bfc\u5165<\/strong>\uff1a\u5c06\u6570\u636e\u5bfc\u51fa\u5230 CSV \u6216 JSON \u7b49\u683c\u5f0f\uff0c\u7136\u540e\u4f7f\u7528\u5177\u6709\u6240\u9700\u6570\u636e\u7c7b\u578b\u7684\u5bfc\u5165\u5de5\u5177\u5c06\u5176\u5bfc\u5165\u56de\u6570\u636e\u5e93\u3002<\/li>\n<\/ul>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<ul>\n<li>\u5728\u8f6c\u6362\u6570\u636e\u7c7b\u578b\u4e4b\u524d\uff0c\u8bf7\u59cb\u7ec8\u5907\u4efd\u60a8\u7684\u6570\u636e\u5e93\u3002<\/li>\n<li>\u786e\u4fdd\u65b0\u6570\u636e\u7c7b\u578b\u4e0e\u6570\u636e\u7684\u9884\u671f\u7528\u9014\u517c\u5bb9\u3002<\/li>\n<li>\u8f6c\u6362\u67d0\u4e9b\u6570\u636e\u7c7b\u578b\uff08\u4f8b\u5982 DATE \u5230 VARCHAR) \u53ef\u80fd\u5bfc\u81f4\u3002<\/li>\n<li>\u5728\u8f6c\u6362\u6d89\u53ca\u5916\u952e\u7ea6\u675f\u7684\u5217\u65f6\uff0c\u9700\u8981\u4ed4\u7ec6\u5904\u7406\u4ee5\u786e\u4fdd\u6570\u636e\u5b8c\u6574\u6027\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u6570\u636e\u7c7b\u578b\u5982\u4f55\u8f6c\u6362\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>\u5728 mysql \u4e2d\u8f6c\u6362\u6570\u636e\u7c7b\u578b\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528 alter table \u8bed\u53e5\u6307\u5b9a\u8981\u66f4\u6539\u7c7b\u578b\u7684\u8868\u548c\u5217\uff1b\u4f7f\u7528 cast() \u51fd\u6570\u4e34\u65f6\u5c06\u8868\u8fbe\u5f0f\u7684\u503c\u8f6c\u6362\u4e3a\u6307\u5b9a\u7684\u7c7b\u578b\uff1b\u521b\u5efa\u5177\u6709\u6240\u9700\u6570\u636e\u7c7b\u578b\u7684\u65b0\u8868\u5e76\u4ece\u65e7\u8868\u4e2d\u9009\u62e9\u6570\u636e\uff1b\u5bfc\u51fa\u6570\u636e\u5e76\u4f7f\u7528\u5bfc\u5165\u5de5\u5177\u5c06\u5176\u5bfc\u5165\u56de\u5177\u6709\u6240\u9700\u6570\u636e\u7c7b\u578b\u7684\u6570\u636e\u5e93\u3002 \u5982\u4f55\u8f6c\u6362 MySQL \u6570\u636e\u7c7b\u578b MySQL \u4e2d\u7684\u6570\u636e\u7c7b\u578b\u51b3\u5b9a\u4e86\u6570\u636e\u5e93\u4e2d\u5b58\u50a8\u6570\u636e\u7684\u683c\u5f0f\u548c\u9650\u5236\u3002\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u8f6c\u6362\u6570\u636e\u7684\u7c7b\u578b\u4ee5\u6ee1\u8db3\u7279\u5b9a\u7684\u9700\u6c42\u6216\u89e3\u51b3\u517c\u5bb9\u6027\u95ee\u9898\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5728 MySQL \u4e2d\u8f6c\u6362\u6570\u636e\u7c7b\u578b\u7684\u6b65\u9aa4\uff1a ALTER TABLE \u8bed\u53e5 \u9996\u5148\uff0c\u9700\u8981\u4f7f\u7528 ALTER TABLE \u8bed\u53e5\u6307\u5b9a\u8981\u66f4\u6539\u7c7b\u578b\u7684\u8868\u548c\u5217\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a ALTER TABLE table_name MODIFY column_name new_data_type; \u767b\u5f55\u540e\u590d\u5236 \u4f8b\u5982\uff0c\u5c06 users \u8868\u4e2d age \u5217\u7684\u6570\u636e\u7c7b\u578b\u4ece INT \u8f6c\u6362\u4e3a VARCHAR(255)\uff1a ALTER TABLE users MODIFY age VARCHAR(255); \u767b\u5f55\u540e\u590d\u5236 CAST() \u51fd\u6570 \u53e6\u4e00\u79cd\u8f6c\u6362\u6570\u636e\u7c7b\u578b\u7684\u65b9\u6cd5\u662f\u4f7f\u7528 CAST() \u51fd\u6570\u3002\u6b64\u51fd\u6570\u53ef\u4ee5\u4e34\u65f6\u5c06\u8868\u8fbe\u5f0f\u7684\u503c\u8f6c\u6362\u4e3a\u6307\u5b9a\u7684\u7c7b\u578b\uff0c\u800c\u65e0\u9700\u6c38\u4e45\u66f4\u6539\u8868\u7ed3\u6784\u3002\u8bed\u6cd5\u5982\u4e0b\uff1a CAST(expression AS new_data_type); \u767b\u5f55\u540e\u590d\u5236 \u4f8b\u5982\uff0c\u5c06 orders [&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-6214","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6214","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=6214"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/6214\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=6214"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=6214"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=6214"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}