請參閱下面的SQL:將2個或更多結果合併在一起?
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
的值從兩個表中的相同日期增加。如何做到這一點?
這似乎很好,謝謝! –