使用sum和count mysql查询连接

时间:2014-08-16 14:57:44

标签: mysql

我有这样的示例数据:

trancation-A

=========================
    date        price
=========================
2014-08-16     50000
2014-08-17     60000
2014-08-16     60000    

trancation-B

=========================
    date        price
=========================
2014-08-16     75000
2014-08-17     90000
2014-08-18     70000    

参数日期=' 2014-08-16'

我想要这样的结果:

=================================
totalA   priceA   totalB  priceB
=================================
2 Unit   110000   1 Unit  75000

我运行我的查询:

select count(A.price)as totalA, sum(A.price)as priceA, 
count(B.price)as totalB,      sum(B.price)as priceB
from t_penjualan as A join t_pembelian as B
where A.tgl_transaksi = '2014-08-16' and B.tgl_transaksi = '2014-08-16'

还是错的...... 请帮帮我....

1 个答案:

答案 0 :(得分:1)

join之前进行聚合

select totalA, priceA, totalB, priceB
from (select count(A.price)as totalA, sum(A.price)as priceA
      from t_penjualan A
      where A.tgl_transaksi = '2014-08-16'
     ) A cross join
     (select count(B.price)as totalB, sum(B.price)as priceB
      from t_pembelian as B
      where B.tgl_transaksi = '2014-08-16'
     ) B;