我有一个这样的示例数据:
交易-A
=========================
date price
=========================
2014-08-16 50000
2014-08-17 60000
2014-08-16 60000
交易-B
=========================
date price
=========================
2014-08-16 75000
2014-08-17 90000
2014-08-18 70000
当参数date='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'
还是错了... 请帮助我......
最佳答案
在加入
之前进行聚合:
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;
关于mysql - 使用 sum 和 count mysql 进行查询连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25341367/