{"id":634,"date":"2023-02-26T11:37:20","date_gmt":"2023-02-26T03:37:20","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=634"},"modified":"2023-04-29T20:00:35","modified_gmt":"2023-04-29T12:00:35","slug":"usage-of-mysql-type-conversion-cast-and-convert-function","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/02\/26\/usage-of-mysql-type-conversion-cast-and-convert-function\/","title":{"rendered":"MySQL\u7c7b\u578b\u8f6c\u6362CAST\u4e0eCONVERT\u51fd\u6570\u7684\u7528\u6cd5"},"content":{"rendered":"<p>MySQL \u7684<code>CAST()<\/code>\u548c<code>CONVERT()<\/code>\u51fd\u6570\u53ef\u7528\u6765\u83b7\u53d6\u4e00\u4e2a\u7c7b\u578b\u7684\u503c\uff0c\u5e76\u4ea7\u751f\u53e6\u4e00\u4e2a\u7c7b\u578b\u7684\u503c\u3002\u4e24\u8005\u5177\u4f53\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-sql\">CAST(value as type);\nCONVERT(value, type);<\/code><\/pre>\n<p><!-- more --><\/p>\n<p>\u5c31\u662f<code>CAST(xxx AS \u7c7b\u578b)<\/code>\uff0c<code>CONVERT(xxx, \u7c7b\u578b)<\/code><\/p>\n<pre><code class=\"language-sql\">mysql&gt; SELECT CAST(&#039;3.35&#039; AS signed);\n+------------------------+\n| CAST(&#039;3.35&#039; AS signed) |\n+------------------------+\n|                      3 |\n+------------------------+\n1 row in set<\/code><\/pre>\n<p>\u53ef\u4ee5\u8f6c\u6362\u7684\u7c7b\u578b\u662f\u6709\u9650\u5236\u7684\u3002\u53ef\u4ee5\u662f\u4ee5\u4e0b\u503c\u5176\u4e2d\u7684\u4e00\u4e2a\uff1a<\/p>\n<ul>\n<li>\u4e8c\u8fdb\u5236\uff0c\u540c\u5e26binary\u524d\u7f00\u7684\u6548\u679c: <code>BINARY<\/code><\/li>\n<li>\u5b57\u7b26\u578b\uff0c\u53ef\u5e26\u53c2\u6570: <code>CHAR()<\/code><\/li>\n<li>\u65e5\u671f: <code>DATE<\/code><\/li>\n<li>\u65f6\u95f4: <code>TIME<\/code><\/li>\n<li>\u65e5\u671f\u65f6\u95f4\u578b: <code>DATETIME<\/code><\/li>\n<li>\u6d6e\u70b9\u6570: <code>DECIMAL<\/code><\/li>\n<li>\u6574\u6570: <code>SIGNED<\/code><\/li>\n<li>\u65e0\u7b26\u53f7\u6574\u6570: <code>UNSIGNED<\/code><\/li>\n<\/ul>\n<p>\u4e0b\u9762\u4e3e\u51e0\u4e2a\u4f8b\u5b50\uff1a<\/p>\n<p>\u4f8b\u4e00<\/p>\n<pre><code class=\"language-sql\">mysql&gt; SELECT CONVERT(&#039;23&#039;, SIGNED);\n+-----------------------+\n| CONVERT(&#039;23&#039;, SIGNED) |\n+-----------------------+\n|                    23 |\n+-----------------------+\n1 row in set<\/code><\/pre>\n<p>\u4f8b\u4e8c<\/p>\n<pre><code class=\"language-sql\">mysql&gt; SELECT CAST(&#039;125e342.83&#039; AS signed);\n+------------------------------+\n| CAST(&#039;125e342.83&#039; AS signed) |\n+------------------------------+\n|                          125 |\n+------------------------------+\n1 row in set<\/code><\/pre>\n<p>\u4f8b\u4e09<\/p>\n<pre><code class=\"language-sql\">mysql&gt; SELECT CAST(&#039;3.35&#039; AS signed);\n+------------------------+\n| CAST(&#039;3.35&#039; AS signed) |\n+------------------------+\n|                      3 |\n+------------------------+\n1 row in set<\/code><\/pre>\n<p>\u5c06varchar \u8f6c\u4e3aint \u7528<code>cast(a as signed)<\/code>\uff0c\u5176\u4e2da\u4e3avarchar\u7c7b\u578b\u7684\u5b57\u7b26\u4e32<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MySQL \u7684CAST()\u548cCONVERT()\u51fd\u6570\u53ef\u7528\u6765\u83b7\u53d6\u4e00\u4e2a\u7c7b\u578b\u7684\u503c\uff0c\u5e76\u4ea7\u751f\u53e6\u4e00\u4e2a\u7c7b\u578b\u7684\u503c\u3002\u4e24\u8005\u5177\u4f53\u7684\u8bed\u6cd5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[],"class_list":["post-634","post","type-post","status-publish","format-standard","hentry","category-mysql"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/634","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=634"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/634\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=634"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=634"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=634"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}