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