mysql - 使用 sum 和 count mysql 进行查询连接

标签 mysql

我有一个这样的示例数据:

交易-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/

相关文章:

mysql - 防止mysql注入(inject)和xss攻击

php - 比较 php 中的 mysql 日期

mysql - 从包含+ 6500万条记录的Mysql连接创建临时表

php - 如何在 PHP 中的 ckeditor 中显示来自 mysql 的数据

javascript - 使用表单提交操作传递 $_GET 变量

mysql - 如何返回数据库中所有表的列表,其中主机名列包含 hostname=hostA

MYSQL查询返回列什么时候应该是值

php - 使用 xampp 和 phpmailer 的奇怪错误

mysql - Ruby mysql gem - 如何在查询时避免日期时间自动转换为字符串

php - 有没有一种正确的方法可以更好地构造数组以进行循环