2015-05-11 14 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 
相关问题