从2个表和每个月分组计数

时间:2013-07-25 07:21:22

标签: mysql sql

我这里有2张桌子基本相同。这是结构。

---------------------------
| Table In                 
---------------------------
| Id    | Date
---------------------------
| 1     | 2013-05-22
| 2     | 2013-07-20
---------------------------


---------------------------
| Table Out                 
---------------------------
| Id    | Date
---------------------------
| 1     | 2013-05-20
| 2     | 2013-06-21
| 3     | 2013-07-24
---------------------------

我只想计算这些数据,预期的结果是:

----------------------------------------------
| month   | countin       | countout
----------------------------------------------
| 5       | 1             | 1
| 6       | 0             | 1
| 7       | 1             | 1

但是,当我尝试使用此查询时:

SELECT month(date) AS `month`, count(*) AS `countin`,
       (SELECT count(*)
        FROM `out`
        WHERE month(date) = `month`) AS `countout`
FROM `in`
GROUP BY `month`

结果是:

----------------------------------------------
| month   | countin       | countout
----------------------------------------------
| 5       | 1             | 1
| 7       | 1             | 1

请帮帮我。

4 个答案:

答案 0 :(得分:4)

用月份加入两个表:

SELECT MONTH(I.date) AS `month`
     , COUNT(I.ID) AS `countin`
     , COUNT(O.ID) AS `countOUT`
  FROM TableIN I
 LEFT JOIN TableOUT O
    ON MONTH(I.Date) = MONTH(O.Date)
 GROUP BY MONTH(I.date)
UNION
SELECT MONTH(O.date) AS `month`
     , COUNT(I.ID) AS `countin`
     , COUNT(O.ID) AS `countOUT`
  FROM TableIN I
 RIGHT JOIN TableOUT O
    ON MONTH(I.Date) = MONTH(O.Date)
 GROUP BY MONTH(I.date);

结果:

| MONTH | COUNTIN | COUNTOUT |
------------------------------
|     5 |       1 |        1 |
|     7 |       1 |        1 |
|     6 |       0 |        1 |

请参阅this SQLFiddle

另外,要按月订购结果,您需要使用如下的子查询:

SELECT * FROM
(
    SELECT MONTH(I.date) AS `month`
         , COUNT(I.ID) AS `countin`
         , COUNT(O.ID) AS `countOUT`
      FROM TableIN I
     LEFT JOIN TableOUT O
        ON MONTH(I.Date) = MONTH(O.Date)
     GROUP BY MONTH(I.date)
    UNION
    SELECT MONTH(O.date) AS `month`
         , COUNT(I.ID) AS `countin`
         , COUNT(O.ID) AS `countOUT`
      FROM TableIN I
     RIGHT JOIN TableOUT O
        ON MONTH(I.Date) = MONTH(O.Date)
     GROUP BY MONTH(I.date)
    ) tbl
ORDER BY Month;

请参阅this SQLFiddle

答案 1 :(得分:1)

select months.mnth,cntin,cntout from
(select month(date) as mnth from TableOUT
union select month(date) as mnth from TableIN) months
left join 
(SELECT month(date) as mnth,count(*) as cntin
        FROM `TableIN` group by month(date)) mntin
on mntin.mnth = months.mnth
left join 
(SELECT month(date) as mnth,count(*) as cntout
        FROM `TableOUT` group by month(date)) mntout
on mntout.mnth = months.mnth
;

fiddle

答案 2 :(得分:1)

MySQL不支持外连接,所以这有点乱:

select ifnull(inmonth, outmonth) as month, countin, countout
from 
(
  Select in.month as inmonth, out.month as outmonth, 
    in.countin as countin, out.countout as countout
  from 
    (select month(`date`) as `month`, count(*) as `countin` from `in` group by month(`date`)) `in`
  left join
    (select month(`date`) as `month`, count(*) as `countout` from `out` group by month(`date`)) `out`
    on in.month=out.month
UNION
 select in.month as inmonth, out.month as outmonth, 
    in.countin as countin, out.countout as countout
  from 
    (select month(`date`) as `month`, count(*) as `countin` from `in` group by month(`date`)) `in`
  right join
    (select month(`date`) as `month`, count(*) as `countout` from `out` group by month(`date`)) `out`
  on in.month=out.month
) as foo

如果表名是transaction_in和transaction_out,则更改

from `in`

from `transaction_in`

使用out s。

答案 3 :(得分:0)

表6中不存在第6行,因此您无法像在不限制数据的情况下那样加入。

我建议这种方法(不需要任何外连接!):

SELECT month,sum(countin),sum(countout) FROM (
SELECT month(date) AS `month`,count(1) AS `countin`,0  AS `countout`
FROM TableIN `in`
GROUP BY `month`
UNION
SELECT month(date) AS `month`,0 `countin`,count(1)  AS `countout`
FROM TableOUT `out`
GROUP BY `month`
) `test`
GROUP BY month

http://sqlfiddle.com/#!2/b967b5/28

也许您可以查看一些外连接机制。 可以找到详细信息here