{"id":203,"date":"2024-12-14T00:11:28","date_gmt":"2024-12-13T16:11:28","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=203"},"modified":"2024-12-14T00:11:28","modified_gmt":"2024-12-13T16:11:28","slug":"%e6%80%8e%e6%a0%b7%e5%a1%ab%e5%86%99-sql-%e6%9f%a5%e8%af%a2%e4%b8%ad%e7%bc%ba%e5%a4%b1%e7%9a%84%e6%97%a5%e6%9c%9f%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2024\/12\/14\/%e6%80%8e%e6%a0%b7%e5%a1%ab%e5%86%99-sql-%e6%9f%a5%e8%af%a2%e4%b8%ad%e7%bc%ba%e5%a4%b1%e7%9a%84%e6%97%a5%e6%9c%9f%ef%bc%9f\/","title":{"rendered":"\u600e\u6837\u586b\u5199 SQL \u67e5\u8be2\u4e2d\u7f3a\u5931\u7684\u65e5\u671f\uff1f"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>\u5728 SQL \u67e5\u8be2\u4e2d\uff0c\u5904\u7406\u7f3a\u5931\u7684\u65e5\u671f\u901a\u5e38\u662f\u4e3a\u4e86\u586b\u8865\u6570\u636e\u96c6\u4e2d\u56e0\u67d0\u4e9b\u65e5\u671f\u7f3a\u5931\u800c\u4ea7\u751f\u7684\u95f4\u9699\uff0c\u5c24\u5176\u662f\u5728\u65f6\u95f4\u5e8f\u5217\u5206\u6790\u4e2d\u3002\u4ee5\u4e0b\u662f\u5b9e\u73b0\u65b9\u6cd5\u7684\u5e38\u89c1\u6b65\u9aa4\u548c\u7b56\u7565\uff1a<\/p>\n<\/blockquote>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1. \u4f7f\u7528\u9012\u589e\u7684\u65e5\u671f\u751f\u6210\u4e00\u4e2a\u5b8c\u6574\u7684\u65e5\u671f\u8868<\/strong><\/h3>\n\n\n\n<p>\u53ef\u4ee5\u624b\u52a8\u6216\u52a8\u6001\u751f\u6210\u4e00\u4e2a\u5305\u542b\u6240\u6709\u65e5\u671f\u7684\u4e34\u65f6\u8868\u6216\u6d3e\u751f\u8868\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f8b\u5b50\uff1a\u751f\u6210\u65e5\u671f\u8303\u56f4<\/h4>\n\n\n\n<p>\u4ee5\u4e0b\u662f\u5982\u4f55\u751f\u6210\u65e5\u671f\u8303\u56f4\u5e76\u4e0e\u539f\u59cb\u6570\u636e\u7ed3\u5408\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">(a) \u5728 MySQL 8.0 \u53ca\u4ee5\u4e0a\u7248\u672c<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>WITH RECURSIVE date_sequence AS (\n    SELECT '2024-01-01' AS date -- \u8d77\u59cb\u65e5\u671f\n    UNION ALL\n    SELECT DATE_ADD(date, INTERVAL 1 DAY)\n    FROM date_sequence\n    WHERE date &lt; '2024-01-31' -- \u7ed3\u675f\u65e5\u671f\n)\nSELECT ds.date, COALESCE(data.value, 0) AS value\nFROM date_sequence ds\nLEFT JOIN your_table data ON ds.date = data.date;<\/code><\/pre>\n\n\n\n<p><strong>\u8bf4\u660e<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>WITH RECURSIVE<\/code> \u7528\u4e8e\u751f\u6210\u4ece\u8d77\u59cb\u65e5\u671f\u5230\u7ed3\u675f\u65e5\u671f\u7684\u8fde\u7eed\u65e5\u671f\u5e8f\u5217\u3002<\/li>\n\n\n\n<li><code>COALESCE<\/code> \u51fd\u6570\u586b\u8865\u7f3a\u5931\u7684\u503c\uff08\u5982\u8bbe\u4e3a 0 \u6216\u5176\u4ed6\u9ed8\u8ba4\u503c\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2. \u4f7f\u7528\u73b0\u6709\u7684\u65e5\u671f\u7ef4\u5ea6\u8868<\/strong><\/h3>\n\n\n\n<p>\u5982\u679c\u6570\u636e\u5e93\u4e2d\u5df2\u6709\u65e5\u671f\u7ef4\u5ea6\u8868\uff0c\u53ef\u4ee5\u76f4\u63a5\u4e0e\u539f\u59cb\u6570\u636e\u8868\u8fdb\u884c\u5173\u8054\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f8b\u5b50\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT d.date, COALESCE(t.value, 0) AS value\nFROM date_dimension d\nLEFT JOIN your_table t ON d.date = t.date\nWHERE d.date BETWEEN '2024-01-01' AND '2024-01-31';<\/code><\/pre>\n\n\n\n<p><strong>\u8bf4\u660e<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>date_dimension<\/code> \u662f\u4e00\u4e2a\u5305\u542b\u6240\u6709\u65e5\u671f\u7684\u8868\uff0c\u53ef\u4ee5\u9884\u5148\u751f\u6210\u5e76\u5b58\u50a8\u3002<\/li>\n\n\n\n<li><code>LEFT JOIN<\/code> \u786e\u4fdd\u5373\u4f7f\u6570\u636e\u8868\u4e2d\u7f3a\u5931\u65e5\u671f\uff0c\u65e5\u671f\u7ef4\u5ea6\u8868\u4e2d\u7684\u65e5\u671f\u4ecd\u4f1a\u663e\u793a\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3. \u4f7f\u7528\u4ea4\u53c9\u8fde\u63a5\u751f\u6210\u65e5\u671f\uff08MySQL 5.7 \u53ca\u4ee5\u4e0b\u7248\u672c\uff09<\/strong><\/h3>\n\n\n\n<p>\u5bf9\u4e8e\u8f83\u65e7\u7684 MySQL \u7248\u672c\uff0c\u53ef\u4ee5\u901a\u8fc7\u8f85\u52a9\u6570\u5b57\u8868\u6765\u751f\u6210\u65e5\u671f\u3002<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u4f8b\u5b50\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT DATE_ADD('2024-01-01', INTERVAL n.num DAY) AS date\nFROM (\n    SELECT 0 AS num UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3\n    UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7\n    UNION ALL SELECT 8 UNION ALL SELECT 9\n) AS n\nLEFT JOIN your_table t ON DATE_ADD('2024-01-01', INTERVAL n.num DAY) = t.date;<\/code><\/pre>\n\n\n\n<p><strong>\u8bf4\u660e<\/strong>\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8f85\u52a9\u6570\u5b57\u8868\u751f\u6210\u4ece <code>2024-01-01<\/code> \u5f00\u59cb\u7684\u8fde\u7eed\u65e5\u671f\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4. \u63d2\u5165\u7f3a\u5931\u65e5\u671f\u5230\u539f\u59cb\u8868\uff08\u5982\u679c\u9700\u8981\u4fee\u6539\u6570\u636e\uff09<\/strong><\/h3>\n\n\n\n<p>\u5982\u679c\u9700\u8981\u76f4\u63a5\u8865\u5168\u539f\u59cb\u8868\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u65b9\u5f0f\uff1a<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">\u67e5\u627e\u7f3a\u5931\u7684\u65e5\u671f\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>WITH RECURSIVE missing_dates AS (\n    SELECT '2024-01-01' AS date\n    UNION ALL\n    SELECT DATE_ADD(date, INTERVAL 1 DAY)\n    FROM missing_dates\n    WHERE date &lt; '2024-01-31'\n)\nSELECT md.date\nFROM missing_dates md\nLEFT JOIN your_table t ON md.date = t.date\nWHERE t.date IS NULL;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">\u63d2\u5165\u7f3a\u5931\u65e5\u671f\uff1a<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>INSERT INTO your_table (date, value)\nSELECT md.date, 0\nFROM (\n    WITH RECURSIVE missing_dates AS (\n        SELECT '2024-01-01' AS date\n        UNION ALL\n        SELECT DATE_ADD(date, INTERVAL 1 DAY)\n        FROM missing_dates\n        WHERE date &lt; '2024-01-31'\n    )\n    SELECT md.date\n    FROM missing_dates md\n    LEFT JOIN your_table t ON md.date = t.date\n    WHERE t.date IS NULL\n) AS missing;<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u52a8\u6001\u751f\u6210\u65e5\u671f\u8303\u56f4<\/strong>\uff1a\u4f7f\u7528 <code>WITH RECURSIVE<\/code> \u6216\u8f85\u52a9\u6570\u5b57\u8868\u3002<\/li>\n\n\n\n<li><strong>\u73b0\u6709\u65e5\u671f\u7ef4\u5ea6\u8868<\/strong>\uff1a\u63a8\u8350\u4e3a\u65f6\u95f4\u5e8f\u5217\u5206\u6790\u9884\u5148\u6784\u5efa\u65e5\u671f\u7ef4\u5ea6\u8868\u3002<\/li>\n\n\n\n<li><strong>\u586b\u8865\u7f3a\u5931\u503c<\/strong>\uff1a\u901a\u8fc7 <code>LEFT JOIN<\/code> \u548c <code>COALESCE<\/code> \u8bbe\u7f6e\u9ed8\u8ba4\u503c\u3002<\/li>\n<\/ul>\n\n\n\n<p>\u9009\u62e9\u5177\u4f53\u65b9\u6cd5\u53d6\u51b3\u4e8e\u6570\u636e\u5e93\u7248\u672c\u548c\u9700\u6c42\u3002\u66f4\u591a\u4fe1\u606f\u8bf7\u5173\u6ce8\u5176\u4ed6\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 SQL \u67e5\u8be2\u4e2d\uff0c\u5904\u7406\u7f3a\u5931\u7684\u65e5\u671f\u901a\u5e38\u662f\u4e3a\u4e86\u586b\u8865\u6570\u636e\u96c6\u4e2d\u56e0\u67d0\u4e9b\u65e5\u671f\u7f3a\u5931\u800c\u4ea7\u751f\u7684\u95f4\u9699\uff0c\u5c24\u5176\u662f\u5728\u65f6\u95f4\u5e8f\u5217\u5206\u6790\u4e2d\u3002\u4ee5 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-203","post","type-post","status-publish","format-standard","hentry","category-sql"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/203","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=203"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/203\/revisions"}],"predecessor-version":[{"id":204,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/203\/revisions\/204"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=203"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=203"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=203"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}