首頁 >資料庫 >mysql教程 >如何檢索單一 MySQL 表中包含空白行的最近 7 天的交易?

如何檢索單一 MySQL 表中包含空白行的最近 7 天的交易?

Barbara Streisand
Barbara Streisand原創
2024-12-31 06:45:10849瀏覽

How to Retrieve Last 7 Days' Transactions with Empty Rows in a Single MySQL Table?

檢索最近 7 天的交易並在 MySQL 單表中包含空行

解決提取最近 7 天交易的挑戰並從 MySQL資料庫擷取空行,我們將遍歷以下內容方法:

  1. 產生日期範圍:
    我們首先使用以下查詢建立過去7 天的日期清單:

    select DATE_FORMAT(a.Date,'%Y-%m-%d') as purchase_date,
    '0' as  amount
    from (
       select curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY as Date
       from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
       cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
       cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
    ) a
    where a.Date BETWEEN NOW() - INTERVAL 7 DAY AND NOW()
  2. 加入交易表:
    接下來,我們將產生的日期清單與交易表連接起來,取得對應的銷售額(如果有):

    SELECT t1.purchase_date,
    coalesce(SUM(t1.amount+t2.amount), 0) AS amount
    from
    (
      select DATE_FORMAT(a.Date,'%Y-%m-%d') as purchase_date,
      '0' as  amount
      from (
         select curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY as Date
         from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
         cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
         cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
      ) a
      where a.Date BETWEEN NOW() - INTERVAL 7 DAY AND NOW()
    )t1
    left join
    (
      SELECT DATE_FORMAT(purchase_date, '%Y-%m-%d') as purchase_date,
      coalesce(SUM(amount), 0) AS amount
      FROM transactions
      WHERE purchase_date BETWEEN NOW() - INTERVAL 7 DAY AND NOW()
      AND vendor_id = 0
      GROUP BY purchase_date
    )t2
    on t2.purchase_date = t1.purchase_date
    group by t1.purchase_date
    order by t1.purchase_date desc

完整查詢:

select 
t1.purchase_date,
coalesce(SUM(t1.amount+t2.amount), 0) AS amount
from
(
  select DATE_FORMAT(a.Date,'%Y-%m-%d') as purchase_date,
  '0' as  amount
  from (
    select curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY as Date
    from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
    cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
    cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
  ) a
  where a.Date BETWEEN NOW() - INTERVAL 7 DAY AND NOW()
)t1
left join
(
  SELECT DATE_FORMAT(purchase_date, '%Y-%m-%d') as purchase_date,
  coalesce(SUM(amount), 0) AS amount
  FROM transactions
  WHERE purchase_date BETWEEN NOW() - INTERVAL 7 DAY AND NOW()
  AND vendor_id = 0
  GROUP BY purchase_date
)t2
on t2.purchase_date = t1.purchase_date
group by t1.purchase_date
order by t1.purchase_date desc

利用此綜合查詢,我們可以提取過去 7 天每一天的總銷售額,有效地包括沒有交易的日子的空行。

以上是如何檢索單一 MySQL 表中包含空白行的最近 7 天的交易?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn