{"id":199669,"date":"2025-05-06T10:48:08","date_gmt":"2025-05-06T02:48:08","guid":{"rendered":"https:\/\/server.hk\/cnblog\/199669\/"},"modified":"2025-05-06T10:48:08","modified_gmt":"2025-05-06T02:48:08","slug":"%e4%bd%bf%e7%94%a8sql%e4%b8%ad%e7%9a%84decimal%e6%95%b0%e6%8d%ae%e7%b1%bb%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/199669\/","title":{"rendered":"\u4f7f\u7528SQL\u4e2d\u7684decimal\u6570\u636e\u7c7b\u578b"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u4f7f\u7528SQL\u4e2d\u7684decimal\u6570\u636e\u7c7b\u578b<\/h1>\n<p>\u6570\u636e\u5e93\u5c0f\u767d\u4e00\u679a\uff0c\u6b63\u5728\u4e0d\u65ad\u5b66\u4e60\u79ef\u7d2f\u77e5\u8bc6\uff0c\u73b0\u5c06\u5b66\u4e60\u5230\u7684\u77e5\u8bc6\u8bb0\u5f55\u4e00\u4e0b\uff0c\u4e5f\u662f\u5c06\u6211\u7684\u6240\u5f97\u5206\u4eab\u7ed9\u5927\u5bb6\uff01\u800c\u4eca\u5929\u8fd9\u7bc7\u6587\u7ae0\u300a\u4f7f\u7528SQL\u4e2d\u7684decimal\u6570\u636e\u7c7b\u578b\u300b\u5e26\u5927\u5bb6\u6765\u4e86\u89e3\u4e00\u4e0b##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u4ece\u800c\u5f25\u8865\u81ea\u5df1\u7684\u4e0d\u8db3\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p><\/p>\n<p>SQL\u4e2d\u7684decimal()\u51fd\u6570\u7684\u4f7f\u7528\u65b9\u6cd5<\/p>\n<p>\u5728SQL\u8bed\u8a00\u4e2d\uff0cdecimal()\u51fd\u6570\u662f\u7528\u6765\u5b9a\u4e49\u4e00\u4e2a\u5177\u6709\u56fa\u5b9a\u7cbe\u5ea6\u548c\u5c0f\u6570\u4f4d\u6570\u7684\u6570\u503c\u7c7b\u578b\u3002\u5b83\u5e38\u7528\u4e8e\u5904\u7406\u9700\u8981\u7cbe\u786e\u8ba1\u7b97\u7684\u6570\u503c\u6570\u636e\uff0c\u5982\u8d27\u5e01\u91d1\u989d\u3001\u767e\u5206\u6bd4\u7b49\u3002\u5728\u672c\u6587\u4e2d\uff0c\u5c06\u4ecb\u7ecddecimal()\u51fd\u6570\u7684\u4f7f\u7528\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<p>decimal()\u51fd\u6570\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n<pre>DECIMAL(precision, scale)<\/pre>\n<p>\u5176\u4e2d\uff0cprecision\u8868\u793a\u603b\u7684\u6709\u6548\u4f4d\u6570\uff0cscale\u8868\u793a\u5c0f\u6570\u70b9\u540e\u7684\u4f4d\u6570\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e9b\u793a\u4f8b\uff0c\u4ee5\u5e2e\u52a9\u7406\u89e3decimal()\u51fd\u6570\u7684\u4f7f\u7528\u3002<\/p>\n<ol>\n<li>\n<p>\u521b\u5efa\u4e00\u4e2a\u5305\u542bdecimal\u7c7b\u578b\u7684\u8868\uff1a<\/p>\n<pre>CREATE TABLE products (\n  id INT,\n  name VARCHAR(100),\n  price DECIMAL(10, 2)\n);<\/pre>\n<p>\u5728\u4e0a\u8ff0\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u540d\u4e3aproducts\u7684\u8868\uff0c\u5176\u4e2d\u7684price\u5217\u5177\u670910\u4f4d\u6709\u6548\u6570\u5b57\u548c2\u4f4d\u5c0f\u6570\u3002<\/p>\n<\/li>\n<li>\n<p>\u63d2\u5165\u6570\u636e\u5230decimal\u7c7b\u578b\u7684\u5217\u4e2d\uff1a<\/p>\n<pre>INSERT INTO products (id, name, price)\nVALUES (1, '\u624b\u673a', 1999.99),\n    (2, '\u7535\u89c6', 2999.00),\n    (3, '\u8033\u673a', 99.95);<\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4e3aproducts\u8868\u4e2d\u7684price\u5217\u63d2\u5165\u4e86\u4e0d\u540c\u7684\u6570\u503c\u6570\u636e\u3002\u8bf7\u6ce8\u610f\uff0c\u63d2\u5165\u7684\u6570\u503c\u5fc5\u987b\u4e0e\u5b9a\u4e49\u7684\u7cbe\u5ea6\u548c\u5c0f\u6570\u4f4d\u6570\u76f8\u5339\u914d\uff0c\u5426\u5219\u5c06\u5f15\u53d1\u9519\u8bef\u3002<\/p>\n<\/li>\n<li>\n<p>\u67e5\u8be2decimal\u7c7b\u578b\u7684\u5217\uff1a<\/p>\n<pre>SELECT * FROM products;<\/pre>\n<p>\u6267\u884c\u4e0a\u8ff0\u67e5\u8be2\u8bed\u53e5\u540e\uff0c\u5c06\u8fd4\u56deproducts\u8868\u4e2d\u7684\u6240\u6709\u8bb0\u5f55\uff0c\u5305\u62ecid\u3001name\u548cprice\u5217\u7684\u503c\u3002<\/p>\n<\/li>\n<li>\n<p>\u5bf9decimal\u7c7b\u578b\u7684\u5217\u8fdb\u884c\u7cbe\u786e\u8ba1\u7b97\uff1a<\/p>\n<pre>SELECT name, price * 0.1 AS discount_price\nFROM products;<\/pre>\n<p>\u4ee5\u4e0a\u793a\u4f8b\u4e2d\uff0c\u4f7f\u7528SELECT\u8bed\u53e5\u8ba1\u7b97\u4e86\u4ee5\u539f\u4ef7\u768410%\u4f5c\u4e3a\u6298\u6263\u540e\u7684\u4ef7\u683c\uff0c\u5e76\u5c06\u8ba1\u7b97\u7ed3\u679c\u547d\u540d\u4e3adiscount_price\u3002\u8fd9\u4e2a\u67e5\u8be2\u53ef\u4ee5\u7528\u4e8e\u8ba1\u7b97\u5546\u54c1\u7684\u6298\u6263\u4ef7\u683c\u3002<\/p>\n<\/li>\n<li>\n<p>\u66f4\u65b0decimal\u7c7b\u578b\u7684\u5217\u7684\u503c\uff1a<\/p>\n<pre>UPDATE products\nSET price = price + 100; <\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528UPDATE\u8bed\u53e5\u5c06products\u8868\u4e2d\u7684price\u5217\u7684\u503c\u589e\u52a0\u4e86100\u3002\u8fd9\u4e2a\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u5bf9decimal\u7c7b\u578b\u7684\u5217\u8fdb\u884c\u66f4\u65b0\u64cd\u4f5c\u3002<\/p>\n<\/li>\n<li>\n<p>\u5220\u9664decimal\u7c7b\u578b\u7684\u5217\uff1a<\/p>\n<pre>ALTER TABLE products\nDROP COLUMN price;<\/pre>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u793a\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u770b\u5230\u5982\u4f55\u4f7f\u7528ALTER TABLE\u8bed\u53e5\u6765\u4ece\u8868\u4e2d\u5220\u9664\u4e00\u4e2a\u5305\u542bdecimal\u7c7b\u578b\u7684\u5217\u3002\u8bf7\u6ce8\u610f\uff0c\u5728\u5220\u9664\u5217\u4e4b\u524d\uff0c\u8bf7\u786e\u4fdd\u5907\u4efd\u91cd\u8981\u6570\u636e\u3002<\/p>\n<\/li>\n<\/ol>\n<p>\u603b\u7ed3\uff1a<br \/>decimal()\u51fd\u6570\u662fSQL\u4e2d\u7528\u4e8e\u5b9a\u4e49\u5177\u6709\u56fa\u5b9a\u7cbe\u5ea6\u548c\u5c0f\u6570\u4f4d\u6570\u7684\u6570\u503c\u7c7b\u578b\u7684\u51fd\u6570\u3002\u901a\u8fc7\u4f7f\u7528\u8be5\u51fd\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u5728\u6570\u636e\u5e93\u4e2d\u5b58\u50a8\u548c\u8ba1\u7b97\u7cbe\u786e\u7684\u6570\u503c\u6570\u636e\u3002\u672c\u6587\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5177\u4f53\u7684\u4ee3\u7801\u793a\u4f8b\uff0c\u4ee5\u5e2e\u52a9\u8bfb\u8005\u7406\u89e3\u8be5\u51fd\u6570\u7684\u4f7f\u7528\u65b9\u6cd5\u3002\u5e0c\u671b\u672c\u6587\u80fd\u5bf9\u8bfb\u8005\u5728SQL\u5f00\u53d1\u4e2d\u4f7f\u7528decimal()\u51fd\u6570\u65f6\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u4f7f\u7528SQL\u4e2d\u7684decimal\u6570\u636e\u7c7b\u578b\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8eSQL,\u4f7f\u7528\u65b9\u6cd5\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u4e3b\u673a\u5b9d\u8d1d\u516c\u4f17\u53f7\uff01<\/p>\n<dl>\n<dt>\n <\/dt>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>\u4f7f\u7528SQL\u4e2d\u7684decimal\u6570\u636e&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-199669","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/199669","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=199669"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/199669\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=199669"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=199669"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=199669"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}