{"id":235232,"date":"2022-02-10T10:38:12","date_gmt":"2022-02-10T02:38:12","guid":{"rendered":"https:\/\/gulass.cn\/?p=235232"},"modified":"2022-01-30T13:39:21","modified_gmt":"2022-01-30T05:39:21","slug":"mysql-sum-null","status":"publish","type":"post","link":"https:\/\/gulass.cn\/mysql-sum-null.html","title":{"rendered":"\u7b80\u5355\u63a2\u8ba8sum()\u51fd\u6570\u8fd4\u56denull\u7684\u95ee\u9898"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\nMySQL\u7684Sum()\u51fd\u6570\u662f\u7528\u6765\u627e\u51fa\u6240\u6709\u8bb0\u5f55\u7684\u4e00\u4e2a\u5b57\u6bb5\u7684\u603b\u548c,\u4e0b\u9762\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u4e86\u5173\u4e8eMysql\u4f7f\u7528sum()\u51fd\u6570\u8fd4\u56denull\u7684\u95ee\u9898,\u6587\u4e2d\u901a\u8fc7\u5b9e\u4f8b\u4ee3\u7801\u4ecb\u7ecd\u7684\u975e\u5e38\u8be6\u7ec6,\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
\u4ecb\u7ecd<\/strong><\/div>\n

SUM()\u51fd\u6570\u7528\u4e8e\u8ba1\u7b97\u4e00\u7ec4\u503c\u6216\u8868\u8fbe\u5f0f\u7684\u603b\u548c\uff0cSUM()\u51fd\u6570\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a<\/p>\n

SUM(DISTINCT expression)<\/pre>\n

SUM()\u51fd\u6570\u662f\u5982\u4f55\u5de5\u4f5c\u7684\uff1f<\/p>\n

\u5982\u679c\u5728\u6ca1\u6709\u8fd4\u56de\u5339\u914d\u884cSELECT\u8bed\u53e5\u4e2d\u4f7f\u7528SUM\u51fd\u6570\uff0c\u5219SUM\u51fd\u6570\u8fd4\u56deNULL\uff0c\u800c\u4e0d\u662f0\u3002<\/p>\n

DISTINCT\u8fd0\u7b97\u7b26\u5141\u8bb8\u8ba1\u7b97\u96c6\u5408\u4e2d\u7684\u4e0d\u540c\u503c\u3002<\/p>\n

SUM\u51fd\u6570\u5ffd\u7565\u8ba1\u7b97\u4e2d\u7684NULL\u503c\u3002<\/p>\n

\u95ee\u9898<\/strong><\/div>\n

\u5728\u9879\u76ee\u4e2d\u4f7f\u7528 Mysql \u4e2d\u7684 sum() \u51fd\u6570\u6c42\u548c\u65f6\uff0c\u8c03\u8bd5\u65f6\u53d1\u73b0\u62a5\u51fa\u4e0b\u9762\u7684\u9519\u8bef\u4fe1\u606f\uff1a<\/p>\n

sql: Scan error on column index 0, name \"total\": converting NULL to int64 is unsupported<\/pre>\n

\u6211\u76f8\u5e94\u6267\u884c\u7684sql\u8bed\u53e5\u5982\u4e0b\uff1a<\/p>\n

SELECT SUM(consume) as total FROM task_income WHERE consume_type=3 AND uid=20;<\/pre>\n
\u9a8c\u8bc1<\/strong><\/div>\n

\u4e3a\u4e86\u627e\u5230\u539f\u56e0\uff0c\u6211\u5728 Navicat \u4e2d\u6267\u884c\u4e86\u4e00\u904d\u51fa\u95ee\u9898\u7684sql\u548c\u53c2\u6570\uff0c\u53d1\u73b0\u7ed3\u679c\u4e2d\u786e\u5b9e\u662f\u8fd4\u56de\u4e86 null :<\/p>\n

\u6309\u6211\u4e4b\u524d\u5f00\u53d1\u65f6\u7684\u60f3\u6cd5\uff1a<\/p>\n

\u5982\u679c\u6709\u76f8\u5e94\u7684\u6570\u636e\uff0c\u8fd4\u56de\u6c42\u548c\u540e\u7684\u503c<\/p>\n

\u5982\u679c\u6ca1\u6709\u7b26\u5408\u6761\u4ef6\u7684\u6570\u636e\uff0c\u8fd4\u56de 0<\/p>\n

\u7ed3\u679c\uff0c\u5f53\u6ca1\u6709\u7b26\u5408\u6761\u4ef6\u7684\u6570\u636e\u65f6\u5374\u8fd4\u56de\u4e86 null \u3002<\/p>\n

\u89e3\u51b3<\/strong><\/div>\n

\u53ef\u4ee5\u901a\u8fc7\u7ed3\u5408\u4f7f\u7528 ISNULL \u6216 COALESCE \u51fd\u6570\u6765\u89e3\u51b3\u3002<\/p>\n

\u76f8\u5e94\u7684\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n

SELECT IFNULL(SUM(Column1), 0) AS total FROM...\r\n \r\nSELECT COALESCE(SUM(Column1), 0) AS total FROM...<\/pre>\n
\u533a\u522b<\/strong><\/div>\n

\u4e0a\u9762\u8bf4\u9053\uff0c\u4f7f\u7528 ISNULL \u6216 COALESCE \u51fd\u6570\u90fd\u53ef\u4ee5\u89e3\u51b3\u4e3a null \u7684\u95ee\u9898\uff0c\u90a3\u4e48\u8fd9\u4e24\u4e2a\u51fd\u6570\u6709\u4ec0\u4e48\u533a\u522b\u5462\uff1f<\/p>\n

ISNULL \u51fd\u6570\u63a5\u53d7\u4e24\u4e2a\u53c2\u6570\uff0c\u5982\u679c\u7b2c\u4e00\u4e2a\u53c2\u6570\u4e0d\u662f null \u4f1a\u76f4\u63a5\u8fd4\u56de\uff0c\u5982\u679c\u7b2c\u4e00\u4e2a\u53c2\u6570\u662f null \uff0c\u5219\u8fd4\u56de\u7b2c\u4e8c\u4e2a\u53c2\u6570<\/p>\n

COALESCE \u51fd\u6570\u53ef\u4ee5\u63a5\u53d7\u4e24\u4e2a\u6216\u591a\u4e2a\u53c2\u6570\uff0c\u5e76\u8fd4\u56de\u7b2c\u4e00\u4e2a\u975e null \u7684\u53c2\u6570\uff0c\u5982\u679c\u6240\u6709\u53c2\u6570\u90fd\u4e3a null\uff0c\u5219\u4f1a\u8fd4\u56de null<\/p>\n

\u53c2\u8003<\/strong><\/div>\n
How do I get SUM function in MySQL to return \u20180\u2019 if no values are found? - Stack Overflow\r\n\r\nsql - What is the difference bewteen ifnull and coalesce in mysql? - Stack Overflow<\/pre>\n

\u5230\u6b64\u8fd9\u7bc7\u5173\u4e8eMysql\u4f7f\u7528sum()\u51fd\u6570\u8fd4\u56denull\u95ee\u9898\u7684\u6587\u7ae0\u5c31\u4ecb\u7ecd\u5230\u8fd9\u4e86<\/p>\n","protected":false},"excerpt":{"rendered":"

SUM()\u51fd\u6570\u7528\u4e8e\u8ba1\u7b97\u4e00\u7ec4\u503c\u6216\u8868\u8fbe\u5f0f\u7684\u603b\u548c\uff0cSUM()\u51fd\u6570\u7684\u8bed\u6cd5\u5982\u4e0b\uff1a SUM(DISTINCT expres […]<\/p>\n","protected":false},"author":362,"featured_media":11367,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-235232","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/235232","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/users\/362"}],"replies":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/comments?post=235232"}],"version-history":[{"count":3,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/235232\/revisions"}],"predecessor-version":[{"id":235235,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/posts\/235232\/revisions\/235235"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media\/11367"}],"wp:attachment":[{"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/media?parent=235232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/categories?post=235232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/gulass.cn\/wp-json\/wp\/v2\/tags?post=235232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}