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

行 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 に答える
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 に答える