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 |
+----------+-------+------------+

它工作得很好,现在我想合并/联合到另一个相同且相同的表结构。 第二条 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 |

如您所见,Total从两个表中的同一日期增加的值。怎么可能呢?

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 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 回答
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 回答
0

试试这个:

最简单的方法是:

您只需将两个结果都放入带有 union all 运算符的派生表中

 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 回答