1

我这里有 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

4 回答 4

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 |

看到这个 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;

看到这个 SQLFiddle

于 2013-07-25T07:26:28.327 回答
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
;

小提琴

于 2013-07-25T07:24:49.263 回答
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`

s也是如此out

于 2013-07-25T07:28:02.787 回答
1

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

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

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

也许你也可以看看一些外部连接机制。详细信息可以在这里找到

于 2013-07-25T07:37:36.107 回答