在不省略行的情况下组合两个查询

时间:2012-11-16 09:39:51

标签: mysql

我有两个表,我需要这两个表中的记录。

查询1

SELECT SUM(rec_issued) AS issed,
       regen_id,
       YEAR(issue_date) AS iYear,
       MONTH(issue_date) AS iMonth
FROM `view_rec_issued`
WHERE `regen_id` = 2
GROUP BY YEAR(issue_date) DESC, MONTH(issue_date) DESC
ORDER BY issue_date ASC

给出了这个结果:

| issed | regen_id | iYear | iMonth |
-------------------------------------
|   424 |        2 |  2011 |      3 |
|  4340 |        2 |  2011 |      4 |
|  4235 |        2 |  2011 |      5 |
| 10570 |        2 |  2012 |      2 |
|  4761 |        2 |  2012 |      3 |
|  5000 |        2 |  2012 |      4 |
|  3700 |        2 |  2012 |      5 |
|  3414 |        2 |  2012 |      6 |
|  3700 |        2 |  2012 |      7 |
|  2992 |        2 |  2012 |      8 |
|   995 |        2 |  2012 |     10 |

查询2

SELECT SUM(total_redem) AS redemed,
       regen_id,
       YEAR(redemption_date) AS rYear,
       MONTH(redemption_date) AS rMonth
FROM `recredem_month_wise`
WHERE `regen_id` = 2
GROUP BY YEAR(redemption_date) DESC, MONTH(redemption_date) DESC
order by redemption_date ASC

给出了这个结果:

| redemed | regen_id | rYear | rMonth |
---------------------------------------
|     424 |        2 |  2011 |      3 |
|     260 |        2 |  2011 |      4 |
|    6523 |        2 |  2011 |      5 |
|    1070 |        2 |  2011 |      6 |
|     200 |        2 |  2011 |     10 |
|     500 |        2 |  2011 |     11 |
|    9750 |        2 |  2012 |      2 |
|    5000 |        2 |  2012 |      3 |
|    5500 |        2 |  2012 |      4 |
|    3803 |        2 |  2012 |      5 |
|    3700 |        2 |  2012 |      7 |
|    3000 |        2 |  2012 |      8 |

期望的结果

但我想这样:

|  issed | regen_id |  iYear | iMonth | redemed | regen_id |  rYear | rMonth |
-------------------------------------------------------------------------------
|    424 |        2 |   2011 |      3 |     424 |        2 |   2011 |      3 |
|   4340 |        2 |   2011 |      4 |     260 |        2 |   2011 |      4 |
|   4235 |        2 |   2011 |      5 |    6523 |        2 |   2011 |      5 |
|   NULL |     NULL |   NULL |   NULL |    1070 |        2 |   2011 |      6 |
|   NULL |     NULL |   NULL |   NULL |     200 |        2 |   2011 |     10 |
|   NULL |     NULL |   NULL |   NULL |     500 |        2 |   2011 |     11 |
|  10570 |        2 |   2012 |      2 |    9750 |        2 |   2012 |      2 |
|   4761 |        2 |   2012 |      3 |    5000 |        2 |   2012 |      3 |
|   5000 |        2 |   2012 |      4 |    5500 |        2 |   2012 |      4 |
|   3700 |        2 |   2012 |      5 |    3803 |        2 |   2012 |      5 |
|   3414 |        2 |   2012 |      6 |    NULL |     NULL |   NULL |   NULL |
|   3700 |        2 |   2012 |      7 |    3700 |        2 |   2012 |      7 |
|   2992 |        2 |   2012 |      8 |    3000 |        2 |   2012 |      8 |
|    995 |        2 |   2012 |     10 |    NULL |     NULL |   NULL |   NULL |

在这些表中,regen_id是唯一的,我需要YEARMONTH的数据。如果一个表在特定的月份和年份中没有任何记录,那么它应该检索零或NULL

但在每个记录年和月都应该像这样 -

 iYear = rYear and iMonth = rMonth

因此我们可以合并两个字段 - 无需显示年份和月份两次

iYear and rYear = year
iMonth and rMonth = month

更新

我尝试了这个查询,几乎解决了问题但是记录没有按照“YEAR DESC,MONTH DESC”的排序形式。

SELECT DISTINCT A.issed, A.regen_id, A.iYear AS yrs, A.iMonth AS mnt, B.redemed, B.regen_id, B.rYear AS yrs, B.rMonth AS mnt
FROM(SELECT SUM(rec_issued) AS issed, regen_id, YEAR(issue_date) AS iYear, MONTH(issue_date) AS iMonth  
FROM `view_rec_issued` 
WHERE `regen_id` = 2 
GROUP BY YEAR(issue_date) DESC, MONTH(issue_date) DESC 
ORDER BY issue_date ASC) AS A 
LEFT JOIN 
(SELECT SUM(total_redem) AS redemed, regen_id, YEAR(redemption_date) AS rYear, MONTH(redemption_date) AS rMonth
FROM `recredem_month_wise` 
WHERE `regen_id` = 2 
GROUP BY YEAR(redemption_date) DESC, MONTH(redemption_date) DESC 
ORDER BY redemption_date ASC) AS B ON A.iYear = B.rYear AND A.iMonth = B.rMonth

UNION

SELECT DISTINCT A.issed, A.regen_id, A.iYear AS yrs, A.iMonth AS mnt, B.redemed, B.regen_id, B.rYear AS yrs, B.rMonth AS mnt
FROM(SELECT SUM(rec_issued) AS issed, regen_id, YEAR(issue_date) AS iYear, MONTH(issue_date) AS iMonth  
FROM `view_rec_issued` 
WHERE `regen_id` = 2 
GROUP BY YEAR(issue_date) DESC, MONTH(issue_date) DESC 
ORDER BY issue_date ASC) AS A 
RIGHT JOIN 
(SELECT SUM(total_redem) AS redemed, regen_id, YEAR(redemption_date) AS rYear, MONTH(redemption_date) AS rMonth 
FROM `recredem_month_wise` 
WHERE `regen_id` = 2 
GROUP BY YEAR(redemption_date) DESC, MONTH(redemption_date) DESC 
ORDER BY redemption_date ASC) AS B ON A.iYear = B.rYear AND A.iMonth = B.rMonth

2 个答案:

答案 0 :(得分:1)

您所描述的内容称为FULL OUTER JOIN:您希望两个表中的所有行,如果找不到匹配项,则另一个表填入NULL。不幸的是,MySQL没有提供完整的外连接,所以你必须模拟它们。

另见Full Outer Join in MySQL

以规范方式模拟全外连接

写这个的一种方法是以下方法,它为缺失的匹配生成NULL

(SELECT I.iYear AS `year`, I.iMonth AS `month`, I.issed, R.redeemed
 FROM (
  SELECT SUM(rec_issued) AS issed,
         regen_id,
         YEAR(issue_date) AS iYear,
         MONTH(issue_date) AS iMonth
  FROM `view_rec_issued`
  WHERE `regen_id` = 2
  GROUP BY YEAR(issue_date), MONTH(issue_date)
 ) I
 LEFT JOIN (
  SELECT SUM(total_redem) AS redemed,
         regen_id,
         YEAR(redemption_date) AS rYear,
         MONTH(redemption_date) AS rMonth
  FROM `recredem_month_wise`
  WHERE `regen_id` = 2
  GROUP BY YEAR(redemption_date), MONTH(redemption_date)
 ) R
 ON (I.iYear = R.rYear AND I.iMonth = R.rMonth)
)
UNION DISTINCT
(SELECT R.iYear AS `year`, R.iMonth AS `month`, I.issed, R.redeemed
 FROM (
  SELECT SUM(total_redem) AS redemed,
         regen_id,
         YEAR(redemption_date) AS rYear,
         MONTH(redemption_date) AS rMonth
  FROM `recredem_month_wise`
  WHERE `regen_id` = 2
  GROUP BY YEAR(redemption_date), MONTH(redemption_date)
 ) R
 LEFT JOIN (
  SELECT SUM(rec_issued) AS issed,
         regen_id,
         YEAR(issue_date) AS iYear,
         MONTH(issue_date) AS iMonth
  FROM `view_rec_issued`
  WHERE `regen_id` = 2
  GROUP BY YEAR(issue_date), MONTH(issue_date)
 ) I
 ON (I.iYear = R.rYear AND I.iMonth = R.rMonth)
)
ORDER BY `year` ASC, `month` ASC

您可以通过为包含的子查询编写视图来缩短这一点,但是如果您希望能够更改regen_id,那么这仍然是相当庞大的代码。

与零结合

你可以通过完全避免外部联接来使事情变得更容易,而使用一个不同的联合,将零填充到这样的行中:

SELECT YEAR(`date`) AS `year`, MONTH(`date`) AS `month`,
       SUM(rec_issued) AS issued, SUM(total_redem) AS redeemed
FROM (
    SELECT issue_date AS `date`, rec_issued, 0 AS total_redem
    FROM view_rec_issued WHERE regen_id = 2
  UNION ALL
    SELECT redemption_date AS `date`, 0 AS rec_issued, total_redem
    FROM redemption_month_wise WHERE regen_id = 2
  ) AS U
GROUP BY EXTRACT(YEAR_MONTH FROM `date`) ASC

将所有内容连接到子查询以查找月份

下表仅使用外部联接模拟来查找在任一表中发生的日期。然后它会离开联接来计算总和。因此,对于缺失值,它将返回零而不是NULL

SELECT LEFT(DI.ym, 4) + 0 AS `year`, RIGHT(DI.ym, 2) + 0 AS `month`,
       DI.issed, SUM(R.total_redem) AS redeemed
FROM
 (SELECT D.ym, SUM(I.rec_issued) AS issed
  FROM
   (SELECT DISTINCT EXTRACT(YEAR_MONTH FROM issue_date) as ym
    FROM view_rec_issued
    WHERE regen_id = 2
   UNION DISTINCT
    SELECT DISTINCT EXTRACT(YEAR_MONTH FROM redemtion_date) AS ym
    FROM recredem_month_wise
    WHERE regen_id = 2
   ) AS D
  LEFT JOIN view_rec_issued AS I
         ON I.regen_id = 2
        AND EXTRACT(YEAR_MONTH FROM I.issue_date) = D.ym
 ) AS DI
LEFT JOIN recredem_month_wise AS R
       ON R.regen_id = 2
      AND EXTRACT(YEAR_MONTH FROM R.redemtion_date) = DI.ym
GROUP BY DI.ym ASC

对于每个表,这将比上述查询多看两次,因此它可能会更慢。如果您担心性能,只需尝试两者并对它们进行基准测试。就代码长度而言,这里的查询更紧凑,我相信也更具可读性。

注意上述查询在past revision of this post中有所不同。该版本省略了子查询DI。结果更好阅读,但遗憾的是错误:如果一个月内有两个问题,还有三个兑换,那么加入会将这个结合到那个月的六行,从而计算每个问题三次每次赎回两次。

到目前为止,所有查询都未经过测试。如果您的问题附带了http://sqlfiddle.com/上的一些示例数据,那么我会在此处发布我的查询,然后再将其发布到此处。

答案 1 :(得分:0)

请按以下顺序尝试:

SELECT * FROM 
(SELECT DISTINCT A.issed, A.regen_id, A.iYear, A.iMonth, B.redemed, B.regen_id, B.rYear, B.rMonth
FROM (SELECT SUM(rec_issued) AS issed, regen_id, YEAR(issue_date) AS iYear, MONTH(issue_date) AS iMonth  
FROM `view_rec_issued` 
WHERE `regen_id` = 2 
GROUP BY YEAR(issue_date) DESC, MONTH(issue_date) DESC 
ORDER BY issue_date ASC) AS A 
LEFT JOIN 
(SELECT SUM(total_redem) AS redemed, regen_id, YEAR(redemption_date) AS rYear, MONTH(redemption_date) AS rMonth 
FROM `recredem_month_wise` 
WHERE `regen_id` = 2 
GROUP BY YEAR(redemption_date) DESC, MONTH(redemption_date) DESC 
ORDER BY redemption_date ASC) AS B ON A.iYear = B.rYear AND A.iMonth = B.rMonth
UNION 
SELECT DISTINCT A.issed, A.regen_id, A.iYear, A.iMonth, B.redemed, B.regen_id, B.rYear, B.rMonth
FROM (SELECT SUM(rec_issued) AS issed, regen_id, YEAR(issue_date) AS iYear, MONTH(issue_date) AS iMonth  
FROM `view_rec_issued` 
WHERE `regen_id` = 2 
GROUP BY YEAR(issue_date) DESC, MONTH(issue_date) DESC 
ORDER BY issue_date ASC) AS A 
RIGHT JOIN 
(SELECT SUM(total_redem) AS redemed, regen_id, YEAR(redemption_date) AS rYear, MONTH(redemption_date) AS rMonth 
FROM `recredem_month_wise` 
WHERE `regen_id` = 2 
GROUP BY YEAR(redemption_date) DESC, MONTH(redemption_date) DESC 
ORDER BY redemption_date ASC) AS B ON A.iYear = B.rYear AND A.iMonth = B.rMonth) AS C
ORDER BY iYear DESC, iMonth DESC