{"id":5483,"date":"2024-11-14T10:18:40","date_gmt":"2024-11-14T02:18:40","guid":{"rendered":"https:\/\/fwq.ai\/blog\/5483\/"},"modified":"2024-11-14T10:18:40","modified_gmt":"2024-11-14T02:18:40","slug":"mysql%e7%b1%bb%e5%9e%8b%e8%bd%ac%e6%8d%a2%e6%80%8e%e4%b9%88%e5%86%99","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/5483\/","title":{"rendered":"mysql\u7c7b\u578b\u8f6c\u6362\u600e\u4e48\u5199"},"content":{"rendered":"<blockquote><p>\n  \u5728 mysql \u4e2d\u5b9e\u73b0\u7c7b\u578b\u8f6c\u6362\u6709\u4ee5\u4e0b\u65b9\u6cd5\uff1acast() \u51fd\u6570\uff1a\u8f6c\u6362\u6570\u636e\u4e3a\u6307\u5b9a\u6570\u636e\u7c7b\u578b\u3002conversion() \u51fd\u6570\uff1a\u63d0\u4f9b\u5bf9\u67d0\u4e9b\u8f6c\u6362\u7684\u9644\u52a0\u652f\u6301\u3002\u9690\u5f0f\u8f6c\u6362\uff1a\u7cfb\u7edf\u81ea\u52a8\u8fdb\u884c\u517c\u5bb9\u7c7b\u578b\u8f6c\u6362\u3002\u663e\u5f0f\u8f6c\u6362\uff1a\u4f7f\u7528\u8fd0\u7b97\u7b26\u5f3a\u5236\u8f6c\u6362\u4e0d\u540c\u7c7b\u578b\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u8f6c\u6362\u9700\u6c42\u548c\u9650\u5236\u3002\n<\/p><\/blockquote>\n<p><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/202405\/29\/2024052903392826152.jpg\" class=\"aligncenter\" title=\"mysql\u7c7b\u578b\u8f6c\u6362\u600e\u4e48\u5199\u63d2\u56fe\" alt=\"mysql\u7c7b\u578b\u8f6c\u6362\u600e\u4e48\u5199\u63d2\u56fe\" \/><\/p>\n<p><strong>\u5982\u4f55\u5728 MySQL \u4e2d\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362<\/strong><\/p>\n<p>\u5728 MySQL \u4e2d\uff0c\u7c7b\u578b\u8f6c\u6362\u662f\u4e00\u79cd\u5c06\u6570\u636e\u4ece\u4e00\u79cd\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u4e00\u79cd\u6570\u636e\u7c7b\u578b\u7684\u65b9\u6cd5\u3002\u5b83\u5141\u8bb8\u60a8\u5c06\u6570\u636e\u6309\u9700\u683c\u5f0f\u5316\u6216\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\u3002<\/p>\n<p><strong>CAST() \u51fd\u6570<\/strong><\/p>\n<p>\u6700\u901a\u7528\u7684\u7c7b\u578b\u8f6c\u6362\u65b9\u6cd5\u662f CAST() \u51fd\u6570\u3002\u6b64\u51fd\u6570\u91c7\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a<\/p>\n<pre>CAST(expression AS data_type)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>expression \u662f\u8981\u8f6c\u6362\u7684\u503c\u3002<\/li>\n<li>data_type \u662f\u8981\u8f6c\u6362\u7684\u76ee\u6807\u6570\u636e\u7c7b\u578b\u3002<\/li>\n<\/ul>\n<p>\u4f8b\u5982\uff0c\u8981\u5c06\u6574\u6570 123 \u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff1a\u201c123\u201d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8be2\uff1a<\/p>\n<pre>SELECT CAST(123 AS CHAR);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>CONVERSION() \u51fd\u6570<\/strong><\/p>\n<p>CONVERSION() \u51fd\u6570\u7c7b\u4f3c\u4e8e CAST() \u51fd\u6570\uff0c\u4f46\u5b83\u5bf9\u67d0\u4e9b\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u63d0\u4f9b\u4e86\u989d\u5916\u7684\u652f\u6301\u3002\u8be5\u51fd\u6570\u91c7\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a<\/p>\n<pre>CONVERSION(value, new_data_type)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u5176\u4e2d\uff1a<\/p>\n<ul>\n<li>value \u662f\u8981\u8f6c\u6362\u7684\u503c\u3002<\/li>\n<li>new_data_type \u662f\u8981\u8f6c\u6362\u7684\u76ee\u6807\u6570\u636e\u7c7b\u578b\u3002<\/li>\n<\/ul>\n<p>\u4f8b\u5982\uff0c\u8981\u5c06\u6d6e\u70b9\u6570 123.45 \u8f6c\u6362\u4e3a\u6574\u6570\uff1a<\/p>\n<pre>SELECT CONVERSION(123.45, INTEGER);<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>\u5176\u4ed6\u7c7b\u578b\u8f6c\u6362\u65b9\u6cd5<\/strong><\/p>\n<p>\u9664\u4e86 CAST() \u548c CONVERSION() \u51fd\u6570\uff0cMySQL \u8fd8\u63d0\u4f9b\u4e86\u5176\u4ed6\u65b9\u6cd5\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362\uff1a<\/p>\n<ul>\n<li> <strong>\uff1a<\/strong>\u5f53\u4e24\u79cd\u6570\u636e\u7c7b\u578b\u517c\u5bb9\u65f6\uff0cMySQL \u4f1a\u81ea\u52a8\u8fdb\u884c\u9690\u5f0f\u8f6c\u6362\u3002\u4f8b\u5982\uff0c\u5c06\u6574\u6570\u548c\u5b57\u7b26\u4e32\u8fde\u63a5\u65f6\uff0c\u6574\u6570\u5c06\u9690\u5f0f\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u3002<\/li>\n<li> <strong>\u663e\u5f0f\u8f6c\u6362\uff1a<\/strong>\u663e\u5f0f\u8f6c\u6362\u4f7f\u7528\u8fd0\u7b97\u7b26\uff08\u5982 +\u3001-\u3001*\u3001\/\uff09\u8fdb\u884c\uff0c\u8fd9\u4e9b\u8fd0\u7b97\u7b26\u53ef\u4ee5\u5f3a\u5236\u8f6c\u6362\u4e0d\u540c\u7c7b\u578b\u7684\u503c\u3002\u4f8b\u5982\uff0c\u5c06\u5b57\u7b26\u4e32\u201c123\u201d\u548c\u6d6e\u70b9\u6570 45.67 \u76f8\u52a0\uff0c\u5b57\u7b26\u4e32\u5c06\u663e\u5f0f\u8f6c\u6362\u4e3a\u6d6e\u70b9\u6570\u3002<\/li>\n<\/ul>\n<p><strong>\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5<\/strong><\/p>\n<p>\u9009\u62e9\u54ea\u79cd\u7c7b\u578b\u8f6c\u6362\u65b9\u6cd5\u53d6\u51b3\u4e8e\u60a8\u8f6c\u6362\u7684\u9700\u8981\u548c\u9650\u5236\u3002<\/p>\n<ul>\n<li> <strong>CAST() \u548c CONVERSION() \u51fd\u6570\uff1a<\/strong>\u8fd9\u4e9b\u51fd\u6570\u63d0\u4f9b\u63a7\u5236\u548c\u7075\u6d3b\u6027\uff0c\u5141\u8bb8\u60a8\u6307\u5b9a\u76ee\u6807\u6570\u636e\u7c7b\u578b\u3002<\/li>\n<li> <strong>\u9690\u5f0f\u8f6c\u6362\uff1a<\/strong>\u5f53\u4e24\u79cd\u7c7b\u578b\u517c\u5bb9\u5e76\u4e14\u60a8\u9700\u8981\u81ea\u52a8\u8f6c\u6362\u65f6\uff0c\u9690\u5f0f\u8f6c\u6362\u5f88\u6709\u7528\u3002<\/li>\n<li> <strong>\u663e\u5f0f\u8f6c\u6362\uff1a<\/strong>\u5f53\u60a8\u9700\u8981\u5f3a\u5236\u8f6c\u6362\u4e0d\u540c\u7c7b\u578b\u7684\u503c\u65f6\uff0c\u663e\u5f0f\u8f6c\u6362\u5f88\u6709\u7528\u3002<\/li>\n<\/ul>\n<p>\u901a\u8fc7\u4e86\u89e3 MySQL \u4e2d\u7684\u4e0d\u540c\u7c7b\u578b\u8f6c\u6362\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u9ad8\u6548\u5730\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u6240\u9700\u683c\u5f0f\uff0c\u4ece\u800c\u8fdb\u884c\u51c6\u786e\u7684\u67e5\u8be2\u548c\u8ba1\u7b97\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u7c7b\u578b\u8f6c\u6362\u600e\u4e48\u5199\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\u5b9e\u73b0\u7c7b\u578b\u8f6c\u6362\u6709\u4ee5\u4e0b\u65b9\u6cd5\uff1acast() \u51fd\u6570\uff1a\u8f6c\u6362\u6570\u636e\u4e3a\u6307\u5b9a\u6570\u636e\u7c7b\u578b\u3002conversion() \u51fd\u6570\uff1a\u63d0\u4f9b\u5bf9\u67d0\u4e9b\u8f6c\u6362\u7684\u9644\u52a0\u652f\u6301\u3002\u9690\u5f0f\u8f6c\u6362\uff1a\u7cfb\u7edf\u81ea\u52a8\u8fdb\u884c\u517c\u5bb9\u7c7b\u578b\u8f6c\u6362\u3002\u663e\u5f0f\u8f6c\u6362\uff1a\u4f7f\u7528\u8fd0\u7b97\u7b26\u5f3a\u5236\u8f6c\u6362\u4e0d\u540c\u7c7b\u578b\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u8f6c\u6362\u9700\u6c42\u548c\u9650\u5236\u3002 \u5982\u4f55\u5728 MySQL \u4e2d\u8fdb\u884c\u7c7b\u578b\u8f6c\u6362 \u5728 MySQL \u4e2d\uff0c\u7c7b\u578b\u8f6c\u6362\u662f\u4e00\u79cd\u5c06\u6570\u636e\u4ece\u4e00\u79cd\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u4e00\u79cd\u6570\u636e\u7c7b\u578b\u7684\u65b9\u6cd5\u3002\u5b83\u5141\u8bb8\u60a8\u5c06\u6570\u636e\u6309\u9700\u683c\u5f0f\u5316\u6216\u8fdb\u884c\u6570\u5b66\u8fd0\u7b97\u3002 CAST() \u51fd\u6570 \u6700\u901a\u7528\u7684\u7c7b\u578b\u8f6c\u6362\u65b9\u6cd5\u662f CAST() \u51fd\u6570\u3002\u6b64\u51fd\u6570\u91c7\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a CAST(expression AS data_type) \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff1a expression \u662f\u8981\u8f6c\u6362\u7684\u503c\u3002 data_type \u662f\u8981\u8f6c\u6362\u7684\u76ee\u6807\u6570\u636e\u7c7b\u578b\u3002 \u4f8b\u5982\uff0c\u8981\u5c06\u6574\u6570 123 \u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff1a\u201c123\u201d\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u67e5\u8be2\uff1a SELECT CAST(123 AS CHAR); \u767b\u5f55\u540e\u590d\u5236 CONVERSION() \u51fd\u6570 CONVERSION() \u51fd\u6570\u7c7b\u4f3c\u4e8e CAST() \u51fd\u6570\uff0c\u4f46\u5b83\u5bf9\u67d0\u4e9b\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u63d0\u4f9b\u4e86\u989d\u5916\u7684\u652f\u6301\u3002\u8be5\u51fd\u6570\u91c7\u7528\u4ee5\u4e0b\u8bed\u6cd5\uff1a CONVERSION(value, new_data_type) \u767b\u5f55\u540e\u590d\u5236 \u5176\u4e2d\uff1a value \u662f\u8981\u8f6c\u6362\u7684\u503c\u3002 new_data_type \u662f\u8981\u8f6c\u6362\u7684\u76ee\u6807\u6570\u636e\u7c7b\u578b\u3002 \u4f8b\u5982\uff0c\u8981\u5c06\u6d6e\u70b9\u6570 123.45 \u8f6c\u6362\u4e3a\u6574\u6570\uff1a SELECT CONVERSION(123.45, INTEGER); \u767b\u5f55\u540e\u590d\u5236 \u5176\u4ed6\u7c7b\u578b\u8f6c\u6362\u65b9\u6cd5 [&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-5483","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5483","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=5483"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/5483\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=5483"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=5483"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=5483"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}