2015-05-11 39 views
1

我從同桌兩個查詢不同之查詢,並加入他們的行列

SELECT userId,SUM(amount) AS totalDeposite FROM trx GROUP BY userId 

輸出:

用戶id totalDeposite


1 470 
2 30 

查詢

SELECT userId,SUM(amount) AS totalFine FROM trx WHERE TYPE='fine' GROUP BY userId 

輸出:

用戶id totalFine


1 20 
2 30 

我怎樣才能像

userId totalDeposite totalFine 
1  470   20 
2  30   30 

回答

4

嘗試結果使用case如下

​​
0

試試這個

select * from 
    (SELECT userId,SUM(amount) AS totalDeposite FROM trx GROUP BY userId) as tD 
    inner join 
    (SELECT userId,SUM(amount) AS totalFine FROM trx WHERE TYPE='fine' GROUP BY userId) 
    tF on td.userId = tf.userId 
相關問題