2014-11-06 62 views
0

我有兩張表。 我需要將這兩個表的每一行組合成表格3中的一行。我設法得到了table1 SUM的金額,但不是table2。MYSQL來自兩個不同的表格組

例如,

表用戶

+---------+-----------+ 
| user_id | user_name | 
+---------+-----------+ 
| 001  | JOHN | 
| 002  | ADAM | 
+---------+-----------+ 

表1

+-----------+----------------+-------------------+---------------------+ 
| table1_id | table1_user_id | table1_amount |  table1_date | 
+-----------+----------------+-------------------+---------------------+ 
| 6  | 001   | 100   | 01/11/2014 10:55 | 
| 7  | 002   | 100    | 01/11/2014 10:55 | 
| 8  | 001   | 50    | 25/10/2014 10:55 | 
| 9  | 001   | 100    | 23/10/2014 11:00 | 
| 10  | 002   | 0    | 21/10/2014 11:00 | 
+-----------+----------------+-------------------+---------------------+ 

表2

+-----------+----------------+----------------+--------------------+ 
| table2_id | table2_user_id | table2_amount | table2_date  | 
+-----------+----------------+----------------+--------------------+ 
| 1  | 001   | 100   | 15/11/2014 10:55 |  
| 2  | 001   | 100   | 15/10/2014 10:55 |  
| 3  | 002   | 100   | 11/10/2014 10:55 |  
| 4  | 001   | 50   | 11/10/2014 10:55 |  
+-----------+----------------+----------------+--------------------+ 

預期結果:

表3

+-----+---------+---------------+---------------+----------+---------+ 
| id | user_id | table1_amount | table2_amount | Year | Month | 
+-----+---------+---------------+---------------+----------+---------+ 
| 1 | 001 | 100  |  100  |  2014 | 11 | 
| 2 | 002 | 100  |  0  |  2014 | 11 | 
| 3 | 001 | 150  |  150  |  2014 | 10 | 
| 4 | 002 |  0   |  100  |  2014 | 10 | 
+-----+---------+---------------+---------------+----------+---------+ 

我的嘗試,但它並沒有顯示預期的結果。每一行table2_amount量是NULL

SQL=" INSERT INTO table3 
     SELECT user_id,SUM(table1_amount),t2.amount2, 
     YEAR(table1_date),MONTH(table1_date) FROM table1 a 
     LEFT JOIN 
    (SELECT c.table2_user_id,SUM(c.table2_amount) as amount2,c.table2_date 
       FROM table2 c 
     GROUP BY DATE_FORMAT(c.table2_date,'%Y-%m'),c.table2_user_id ASC 
      ) t2 
    on t2.table2_user_id = a.table1_user_id AND t2.table2_date = a.table1_date 
    GROUP BY DATE_FORMAT(a.table1_date,'%Y-%m'),table1_user_id ASC "; 
" 
+0

你是什麼意思「它不工作」?你有錯誤嗎?你的結果不好嗎?詳情請 – 2014-11-06 09:40:02

+0

對不起,它沒有顯示預期的結果。 @no想法名稱 – xyonme 2014-11-06 09:41:15

+0

你爲什麼使用左連接?以及桌子如何加入? – 2014-11-06 09:44:18

回答

1

這是一個很好的任務UNION

SELECT tx.uid,SUM(tx.a1),SUM(tx.a2),YEAR(tx.d),MONTH(tx.d) 
FROM 
(
SELECT t1.table1_user_id as uid, 
    t1.table1_amount as a1, 
    0 as a2, 
    t1.table1_date as d 
    FROM table1 t1 
UNION 
SELECT t2.table2_user_id as uid, 
    0 as a1, 
    t2.table2_amount as a2, 
    t2.table2_date as d 
    FROM table2 t2 
) tx 
GROUP BY DATE_FORMAT(d,'%Y-%m'),uid ASC 
+0

嗨大衛。 Thnak你的答案,但你應該知道table1_id和table2_id是不一樣的,他們可能沒有相同數量的行。我已經應用了你的SQL,但是我得到了這個錯誤'「#1222 - 使用的SELECT語句有不同數量的列」' – xyonme 2014-11-06 10:15:04

+0

這對聯合並不重要,你確定你完全複製了查詢嗎?這個錯誤意味着你不得不錯過這兩個子查詢選擇中的一列。 – David162795 2014-11-06 10:19:04

+0

實際上表格id是不需要的,因爲它沒有被使用,只是用戶id。我從我的答案中刪除了它。 – David162795 2014-11-06 10:20:33

0

感謝David162795的啓發討論。

當兩個表中的日期不同時,錯過的點是按日期和用戶標識的INNER QUERY組。 我們需要在內部查詢中將它們按各自的日期分組,然後按時間變量對主要SELECT查詢進行分組。

這正好我對這種情況下的答案:

$SQL = " 
INSERT INTO table3 (user_id, table1_amount, table2_amount,Year, Month) 
SELECT tx.uid, SUM(tx.sum1), SUM(tx.sum2),YEAR(tx.d) as year,MONTH(tx.d) as month 
    FROM 
    (SELECT b.table1_user_id as uid,b.table1_amount as sum1,0 as sum2, 
      b.table1_date as d FROM table1 b 
    GROUP BY DATE_FORMAT(d,'%Y-%m'),uid ASC 

    UNION 

    SELECT c.table2_user_id as uid,0 as sum1, 
      sum(c.table2_amount) as sum2,c.table2_date as d1 
       FROM table2 c 
    GROUP BY DATE_FORMAT(d1,'%Y-%m'),uid ASC 
) tx 

GROUP BY year,month,uid"