[英]Find missing dates in a month of april in mysql
我幾乎是mysql的新手,正在嘗試解決此查詢。 我有兩張桌子
一個是month_april,第二個是month_sales
Month_april表
Month_dates
2015-04-01
2015-04-02
2015-04-03
2015-04-04
2015-04-05
2015-04-06
.
.
.
.
2015-04-30
月銷售表
Month_dates Sale_name
2015-04-01 Remote
2015-04-05 Remote
2015-04-08 Remote
2015-04-09 Remote
2015-04-10 Remote
2015-04-15 Remote
2015-04-20 Remote
2015-04-25 Remote
2015-04-30 Remote
這是兩個表,並且只有一個產品名為“ Remote”。 我需要帶有“銷售名稱”的“月份銷售”表中缺少日期的數據
僅使用month_sales表可以獲取數據嗎? 如果沒有,那我怎樣才能使用兩個表。
我正在尋找這樣的輸出
Month_dates Sale_name
2015-04-02 Remote
2015-04-03 Remote
2015-04-04 Remote
2015-04-06 Remote
2015-04-07 Remote
2015-04-11 Remote
2015-04-12 Remote
2015-04-13 Remote
2015-04-14 Remote
2015-04-16 Remote
2015-04-17 Remote
2015-04-18 Remote
2015-04-19 Remote
2015-04-21 Remote
2015-04-22 Remote
2015-04-23 Remote
2015-04-24 Remote
2015-04-26 Remote
2015-04-27 Remote
2015-04-28 Remote
2015-04-29 Remote
這是您想要的嗎?
select ma.month_date, 'Remote' as sale_name
from month_april ma left join
month_sales ms
on ma.month_date = ms.month_date
where ms.month_date is null;
您可以在日期表上left join
以查找缺少的日期。
select a.month_dates, s.sales_name
from month_april a
left join month_sales s
on a.month_dates <> s.month_dates
where s.month_dates is not null;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.