{"id":2725,"date":"2023-10-17T10:53:21","date_gmt":"2023-10-17T02:53:21","guid":{"rendered":"https:\/\/white-night.club\/?p=2725"},"modified":"2023-11-09T11:07:49","modified_gmt":"2023-11-09T03:07:49","slug":"ad62","status":"publish","type":"post","link":"https:\/\/white-night.club\/index.php\/2023\/10\/17\/ad62\/","title":{"rendered":"1677.\u00a0\u53d1\u7968\u4e2d\u7684\u4ea7\u54c1\u91d1\u989d"},"content":{"rendered":"\n<p><code>Product<\/code>&nbsp;\u8868\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+-------------+---------+\n| Column Name | Type    |\n+-------------+---------+\n| product_id  | int     |\n| name        | varchar |\n+-------------+---------+\nproduct_id \u662f\u8fd9\u5f20\u8868\u7684\u4e3b\u952e\n\u8868\u4e2d\u542b\u6709\u4ea7\u54c1 id \u3001\u4ea7\u54c1\u540d\u79f0\u3002\u4ea7\u54c1\u540d\u79f0\u90fd\u662f\u5c0f\u5199\u7684\u82f1\u6587\u5b57\u6bcd\uff0c\u4ea7\u54c1\u540d\u79f0\u90fd\u662f\u552f\u4e00\u7684\n<\/pre>\n\n\n\n<p><code>Invoice <\/code>\u8868\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+-------------+------+\n| Column Name | Type |\n+-------------+------+\n| invoice_id  | int  |\n| product_id  | int  |\n| rest        | int  |\n| paid        | int  |\n| canceled    | int  |\n| refunded    | int  |\n+-------------+------+\ninvoice_id \u53d1\u7968 id \uff0c\u662f\u8fd9\u5f20\u8868\u7684\u4e3b\u952e\nproduct_id \u4ea7\u54c1 id\nrest \u5e94\u7f34\u6b3e\u9879\npaid \u5df2\u652f\u4ed8\u91d1\u989d\ncanceled \u5df2\u53d6\u6d88\u91d1\u989d\nrefunded \u5df2\u9000\u6b3e\u91d1\u989d\n<\/pre>\n\n\n\n<p>\u8981\u6c42\u5199\u4e00\u4e2aSQL\u67e5\u8be2\uff0c\u5bf9\u4e8e\u6240\u6709\u4ea7\u54c1\uff0c\u8fd4\u56de\u6bcf\u4e2a\u4ea7\u54c1\u7684\u4ea7\u54c1\u540d\u79f0\uff0c\u4ee5\u53ca\u5168\u90e8\u53d1\u7968\u7d2f\u8ba1\u7684\u603b\u5e94\u7f34\u6b3e\u9879\u3001\u603b\u5df2\u652f\u4ed8\u91d1\u989d\u3001\u603b\u5df2\u53d6\u6d88\u91d1\u989d\u3001\u603b\u5df2\u9000\u6b3e\u91d1\u989d\u3002<\/p>\n\n\n\n<p>\u67e5\u8be2\u7ed3\u679c\u6309&nbsp;<code>product_name<\/code>&nbsp;\u6392\u5e8f<\/p>\n\n\n\n<p>\u793a\u4f8b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">Product \u8868\uff1a\n+------------+-------+\n| product_id | name  |\n+------------+-------+\n| 0          | ham   |\n| 1          | bacon |\n+------------+-------+\nInvoice table:\n+------------+------------+------+------+----------+----------+\n| invoice_id | product_id | rest | paid | canceled | refunded |\n+------------+------------+------+------+----------+----------+\n| 23         | 0          | 2    | 0    | 5        | 0        |\n| 12         | 0          | 0    | 4    | 0        | 3        |\n| 1          | 1          | 1    | 1    | 0        | 1        |\n| 2          | 1          | 1    | 0    | 1        | 1        |\n| 3          | 1          | 0    | 1    | 1        | 1        |\n| 4          | 1          | 1    | 1    | 1        | 0        |\n+------------+------------+------+------+----------+----------+\nResult \u8868\uff1a\n+-------+------+------+----------+----------+\n| name  | rest | paid | canceled | refunded |\n+-------+------+------+----------+----------+\n| bacon | 3    | 3    | 3        | 3        |\n| ham   | 2    | 4    | 5        | 3        |\n+-------+------+------+----------+----------+\n- bacon \u7684\u603b\u5e94\u7f34\u6b3e\u9879\u4e3a 1 + 1 + 0 + 1 = 3\n- bacon \u7684\u603b\u5df2\u652f\u4ed8\u91d1\u989d\u4e3a 1 + 0 + 1 + 1 = 3\n- bacon \u7684\u603b\u5df2\u53d6\u6d88\u91d1\u989d\u4e3a 0 + 1 + 1 + 1 = 3\n- bacon \u7684\u603b\u5df2\u9000\u6b3e\u91d1\u989d\u4e3a 1 + 1 + 1 + 0 = 3\n- ham \u7684\u603b\u5e94\u7f34\u6b3e\u9879\u4e3a 2 + 0 = 2\n- ham \u7684\u603b\u5df2\u652f\u4ed8\u91d1\u989d\u4e3a 0 + 4 = 4\n- ham \u7684\u603b\u5df2\u53d6\u6d88\u91d1\u989d\u4e3a 5 + 0 = 5\n- ham \u7684\u603b\u5df2\u9000\u6b3e\u91d1\u989d\u4e3a 0 + 3 = 3<\/pre>\n\n\n\n<div style=\"height:60px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<blockquote class=\"wp-block-quote has-luminous-vivid-amber-color has-text-color\">\n<p>\u89e3\u9898\u601d\u8def<\/p>\n<cite><em>IFNULL<\/em><\/cite><\/blockquote>\n\n\n\n<p>\u770b\u5230\u8fd9\u4e2a\u7b80\u5355\u9898\u901a\u8fc7\u7387\u597d\u4f4e\uff0c\u5199\u4e86\u4e0b\u53d1\u73b0\uff0c\u8fd9SQL\u4e5f\u592a\u957f\u4e86\uff0c\u602a\u4e0d\u5f97\u6ca1\u4eba\u5199\u3002\u4e0d\u8fc7\u7406\u89e3\u8d77\u6765\u8fd8\u86ee\u7b80\u5355\u7684\uff0c\u6709\u7684\u4ea7\u54c1\u53ef\u80fd\u6ca1\u6709\u53d1\u7968\u4fe1\u606f\uff0c\u6240\u4ee5\u8981\u7528ifnull\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Write your MySQL query statement below\r\nSELECT A.name,IFNULL(SUM(B.rest),0) AS rest,\r\nIFNULL(SUM(B.paid),0) AS paid,\r\nIFNULL(SUM(B.canceled),0) AS canceled,\r\nIFNULL(SUM(B.refunded),0) AS refunded\r\nFROM Product AS A\r\nLEFT JOIN Invoice AS B \r\nON A.product_id=B.product_id\r\nGROUP BY A.product_id\r\nORDER BY A.name ASC<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Leetcode\u9898\u76ee\uff1aIFNULL<\/p>\n","protected":false},"author":1,"featured_media":2827,"comment_status":"open","ping_status":"","sticky":false,"template":"base","format":"standard","meta":{"footnotes":""},"categories":[42,25],"tags":[38],"_links":{"self":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/2725"}],"collection":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/comments?post=2725"}],"version-history":[{"count":0,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/posts\/2725\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media\/2827"}],"wp:attachment":[{"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/media?parent=2725"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/categories?post=2725"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/white-night.club\/index.php\/wp-json\/wp\/v2\/tags?post=2725"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}