\u4e0a\u9762\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u60c5\u51b5\uff0c\u4f46\u662f\u5982\u679c\u9047\u5230\u4e0b\u9762\u8fd9\u6837\u7684\u805a\u5408\u51fd\u6570\u5462?<\/p>\n
SELECT SUM(A)\/COUNT(B) FROM TAB<\/pre>\n\u9047\u5230\u8fd9\u6837\u7684\u60c5\u51b5CASE WHEN \u662f\u4e0d\u80fd\u5224\u65adCOUNT(B)\u7684\u503c\u7684\uff0c\u56e0\u4e3aWHEN\u540e\u9762\u7684\u6761\u4ef6\u4e0d\u80fd\u4f7f\u7528\u805a\u5408\u51fd\u6570(\u8bed\u6cd5\u8981\u6c42)\uff0c\u8fd9\u4e2a\u65f6\u5019\u6211\u4eec\u53ef\u4ee5\u8fd9\u6837\u5904\u7406<\/p>\n
SELECT \r\nISNULL(SUM(A)\/NULLIF(COUNT(B),0),0) \r\nFROM TAB <\/pre>\n\u5176\u4e2d\u8fd9\u91cc\u4f7f\u7528\u4e86\u4e24\u4e2a\u51fd\u6570\uff0cNULLIF()\u548cISNULL()<\/p>\n
NULLIF\u51fd\u6570\u6709\u4e24\u4e2a\u53c2\u6570,\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n
NULLIF( expression1 , expression2 )<\/p>\n
\u5176\u4f5c\u7528\u5c31\u662f\uff1a\u5982\u679c\u4e24\u4e2a\u6307\u5b9a\u7684\u8868\u8fbe\u5f0f\u76f8\u7b49\uff0c\u5c31\u8fd4\u56deNULL\u503c\u3002<\/p>\n
ISNULL\u51fd\u6570\u4e5f\u6709\u4e24\u4e2a\u53c2\u6570\uff0c\u5b9a\u4e49\u5982\u4e0b\uff1a<\/p>\n
ISNULL( expression1 , expression2 )<\/p>\n
\u5176\u4f5c\u7528\u662f\uff1a\u5982\u679c\u7b2c\u4e00\u4e2a\u53c2\u6570\u7684\u7ed3\u679c\u4e3aNULL\uff0c\u5c31\u8fd4\u56de\u7b2c\u4e8c\u4e2a\u53c2\u6570\u7684\u503c\u3002<\/p>\n
\u5f53COUNT(B)\u7684\u7ed3\u679c\u4e3a0\u65f6\uff0c\u6070\u597d\u4e0e\u7b2c\u4e8c\u4e2a\u7ed9\u5b9a\u7684\u53c2\u65700\u76f8\u7b49\uff0c\u8fd9\u4e2a\u65f6\u5019NULLIF\u51fd\u6570\u5c31\u4f1a\u8fd4\u56deNULL\uff0c\u800cSUM(A)\u5728\u9664\u4ee5NULL\u65f6\u7ed3\u679c\u4e3aNULL\uff0c\u5916\u5c42\u4f7f\u7528ISNULL\u51fd\u6570\u518d\u5bf9NULL\u503c\u8fdb\u884c\u5224\u65ad\uff0c\u8fd9\u6837\u6700\u7ec8\u7ed3\u679c\u5c31\u662f0\u4e86\u3002<\/p>\n
\u8fd9\u4e24\u79cd\u65b9\u6cd5\u5c31\u662f\u6211\u4eec\u65e5\u5e38\u5904\u7406\u9664\u6570\u4e3a0\u7684\u60c5\u51b5\u4e86\uff0c\u4e00\u5b9a\u8981\u8bb0\u5f97\u54e6\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"
\u4f8b\u5982 SELECT A\/B FROM TAB \u9047\u5230\u8fd9\u6837\u7684\u60c5\u51b5\uff0c\u4e00\u822c\u7684\u5904\u7406\u65b9\u6cd5\u662f\u7528CASE WHEN\u6765\u5224\u65adB\u7684\u503c […]<\/p>\n","protected":false},"author":1920,"featured_media":208759,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-224882","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/224882","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/1920"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=224882"}],"version-history":[{"count":4,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/224882\/revisions"}],"predecessor-version":[{"id":225036,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/224882\/revisions\/225036"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/208759"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=224882"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=224882"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=224882"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}