0

次の SQL を参照してください。

SELECT M.username, count(*) as Total, date(status_date) as Date FROM com_result
   LEFT JOIN members as M on M.member_id = com_result.member_id 
GROUP BY date(status_date), com_result.member_id  Order By status_date DESC

DATEこれにより、とに基づくレコードの総数が表示されますmember_id

結果の例:

+----------+-------+------------+
| username | Total | DATE       |
+----------+-------+------------+
| bx7      |     3 | 2012-09-10 |
| bx2      |    25 | 2012-09-04 |
| bx2      |   401 | 2012-09-03 |
| bx1      |   703 | 2012-09-02 |
| bx4      |  1075 | 2012-09-02 |
+----------+-------+------------+

それはうまく機能します。今、私は別の同じ同じテーブル構造にマージ/ユニオンしたいと考えています。 2 番目の SQL:

SELECT M.username, count(*) as Total, date(status_date) as Date FROM com_result_b
   LEFT JOIN members as M on M.member_id = com_result_b.member_id 
GROUP BY date(status_date), com_result_b.member_id  Order By status_date DESC

からの結果が次のようになると仮定しcom_resultます。

| bx2   |    25 | 2012-09-04 |

からの結果は次のcom_result_bようになります。

 | bx2   |    50 | 2012-09-04 |

したがって、マージ/ユニオンすると、結果は次のようになります。

 | bx2   |    75 | 2012-09-04 |

Total2 つのテーブルから同じ日付から増加した値を確認できます。どうすればそれができますか?

4

4 に答える 4

2
SELECT
  username,
  IF(q1.Total IS NULL,0,q1.Total)+IF(q2.Total IS NULL,0,q2.Total) AS Total,
  q1.Date
FROM (
  SELECT member_id, M.username, count(*) as Total, date(status_date) as Date 
  FROM com_result
  LEFT JOIN members as M on M.member_id = com_result.member_id 
  GROUP BY date(status_date), com_result.member_id  
) AS q1
LEFT JOIN (
  SELECT member_id, M.username, count(*) as Total, date(status_date) as Date 
  FROM com_result_b
  LEFT JOIN members as M on M.member_id = com_result_b.member_id 
  GROUP BY date(status_date), com_result_b.member_id
) AS q2 ON q1.member_id=q2.member_id AND q1.Date=q2.Date
ORDER BY q1.Date DESC
于 2012-09-10T11:18:24.887 に答える
1
SELECT X.username, SUM(X.Total)  AS TOTAL , X.Date
FROM( SELECT ... FROM com_result Union Select .. From com_result_b)X
GROUP BY X.username,  X.Date
于 2012-09-10T11:15:54.463 に答える
1

SUMで

select username,SUM(Total) , DATE
(
    SELECT M.username, count(*) as Total, date(status_date) as Date FROM com_result_b 
       LEFT JOIN members as M on M.member_id = com_result.member_id  
    GROUP BY date(status_date), com_result.member_id  
   UNION ALL
   SELECT M.username, count(*) as Total, date(status_date) as Date FROM com_result_b   
      LEFT JOIN members as M on M.member_id = com_result_b.member_id    
   GROUP BY date(status_date), com_result_b.member_id 
) v

group by username, date
于 2012-09-10T11:15:46.103 に答える
0

これを試して:

最も簡単な方法は次のとおりです。

両方の結果をユニオンオール演算子を使用して派生テーブルに入れるだけです

 SELECT username,SUM(Total),Date
FROM   (
        SELECT M.username, count(*) as Total, date(status_date) as Date 
        FROM com_result
        LEFT JOIN members as M on M.member_id = com_result.member_id 
        GROUP BY date(status_date), com_result.member_id   
        union all
        SELECT M.username, count(*) as Total, date(status_date) as Date 
        FROM com_result_b
        LEFT JOIN members as M on M.member_id = com_result_b.member_id 
        GROUP BY date(status_date), com_result_b.member_id )a
GROUP BY username,Date
ORDER BY Date DESC
于 2012-09-10T11:15:33.533 に答える