簡體   English   中英

兩個結果集的MySql聯合

[英]MySql Union of two result sets

我有兩個結果集,如下所示。 我想用第一個結果的NULL替換為第二個結果集中的值的合並結果。

[編輯-為清楚起見添加]

第一個結果集是按月收集和匯總的實際數據。 如果您看到第一個表的行是每個月。 它們來自另一個收集歷史數據的表。 第二個結果集是下個月的預測值。 我希望能夠在可用的地方顯示實際值,並在“空”的地方填充預測值。

mysql> SELECT sf.date, sf.col1, sf.col2, sf.col3, sf.col4, sf.col5, sf.col6, sf.col7, sf.col8 FROM sf_table AS sf order by sf.date desc limit 3;              
+------------+------+-------+----------+-------+---------+------+------+--------+                       
| date       | col1 | col2  | col3     | col4  | col5    | col6 | col7 | col8   |
+------------+------+-------+----------+-------+---------+------+------+--------+ 
| 2015-12-01 | 5.00 | 48.20 |     NULL |  NULL |    NULL | 2.24 | 0.24 |   NULL | 
| 2015-11-01 | 5.00 | 48.60 | 12288.50 | 69344 | 1282.00 | 2.26 | 0.12 | 238.11 | 
| 2015-10-01 | 5.00 | 50.10 | 12201.40 | 69584 | 1161.00 | 2.07 | 0.12 | 238.04 |
+------------+------+-------+----------+-------+---------+------+------+------

mysql> SELECT mai.date, mai.col1, mai.col2, mai.col3, mai.col4, mai.col5, mai.col6, mai.col7 , mai.col8 FROM vw_month_ahead as mai order by ai.date desc limit 3;       
+------------+----------+-----------+--------------+--------------+-------------+----------+----------+------------+ 
| date       | col1     | col2      | col3         | col4         | col5        | col6     | col7     | col8       |
+------------+----------+-----------+--------------+--------------+-------------+----------+----------+------------+ 
| 2016-01-29 | 4.951949 | 47.433569 | 12379.161917 | 69778.974145 | 1369.281614 | 2.283758 | 0.229009 | 238.427982 |
+------------+----------+-----------+--------------+--------------+-------------+----------+----------+------------+

如何獲得以下內容?

+------------+----------+-----------+--------------+--------------+-------------+----------+----------+------------+
| date       | col1     | col2      | col3         | col4         | col5        | col6     | col7     | col8       |
+------------+----------+-----------+--------------+--------------+-------------+----------+----------+------------+
| 2015-12-01 | 5.00     | 48.20     | 12379.161917 | 69778.974145 | 1369.281614 | 2.24     | 0.24     | 238.427982 |
+------------+----------+-----------+--------------+--------------+-------------+----------+----------+------------+

請注意:Col3,Col4,Col5和Col8來自第二個結果集(vw_month_ahead),其中其他是第一個結果集(sf_table)的最上一行。

也許是這樣的:

SELECT
      sf.date
    , coalesce(sf.col1,mai.col1)
    , coalesce(sf.col2,mai.col2)
    , coalesce(sf.col3,mai.col3)
    , coalesce(sf.col4,mai.col4)
    , coalesce(sf.col5,mai.col5)
    , coalesce(sf.col6,mai.col6)
    , coalesce(sf.col7,mai.col7)
    , coalesce(sf.col8,mai.col8)
FROM sf_table AS sf
left join vw_month_ahead AS mai 
  on sf.date = date_add(date_add(mai.date,INTERVAL -(day(mai.date)-1) DAY),INTERVAL -1 MONTH)
ORDER BY
      sf.date DESC limit 3;              

如果可用,它將顯示來自sf_table的數據,如果不可用,則顯示來自vw_month_ahead的數據。

[編輯]請注意,上面提出的加入條件將日期減少到一個月的第一天,然后減去一個月。 例如2016-01-29變為2016-01-01,然后變為2015-12-01,以便可以與sf_table日期對齊。

如果兩個表中沒有可用數據,則可以返回零:

SELECT
      sf.date
    , coalesce(sf.col1,mai.col1,0)
    , coalesce(sf.col2,mai.col2,0)
...

由於注釋將mai描述為僅包含一行,因此您將執行以下操作:

SELECT sf.date, IFNULL(sf.col1, mai.col1) col1, IFNULL(sf.col2, mai.col2) col2, ...
FROM sf_table AS sf
LEFT JOIN vw_month_ahead AS mai
ORDER BY sf.date DESC;

該查詢將兩個表連接在一起,並且sf中的值為null時,使用mai的值(如果mai中沒有行,則可以為null)。 請注意,對於要以這種方式合並的每一列,您將必須重復IFNULL()列描述。 是的,這很乏味,但是會起作用。

如果需要將此擴展到多個表,則可以使用COALESCE()代替IFNULL()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM