Mysql Sum不同的行

时间:2012-09-21 16:08:36

标签: mysql left-join aggregate-functions

我想总结3个表中3列的值

我的查询

SELECT sum(s.paid_money+d.paid_money+e.paid_money)
       FROM payment_dates AS dates
       LEFT JOIN supplier_payments AS s 
                ON dates.id = s.payment_date_id
       LEFT JOIN daily_payments AS d 
                ON dates.id = d.payment_date_id
       LEFT JOIN emp_payments AS e 
                ON dates.id = e.payment_date_id;

当我通过以下查询从3个表中选择 paid_money 时:

 SELECT dates.id, s.paid_money as sup, d.paid_money as daily, e.paid_money as emp  
       FROM payment_dates AS dates  
       LEFT JOIN supplier_payments AS s 
                ON dates.id = s.payment_date_id
       LEFT JOIN daily_payments AS d 
                ON dates.id = d.payment_date_id
       LEFT JOIN emp_payments AS e 
                ON dates.id = e.payment_date_id;

- 输出
 id sup daily emp
'1',NULL,NULL,NULL
'2','75','37',NULL
'2','75','255',NULL
'2','669','37',NULL
'2','669','255',NULL
'3','575','65',NULL
'3','575','909',NULL
'3','575','160',NULL
'3','575','60',NULL
'3','575','100',NULL
'3','575','624.5',NULL
'3','575','70',NULL
'3','423','65',NULL
'3','423','909',NULL
'3','423','160',NULL
'3','423','60',NULL
'3','423','100',NULL
'3','423','624.5',NULL
'3','423','70',NULL

  • 例如Id=2 duplicate 4 time
  • 和sup值为75重复两次,669重复两次。
  • id=35757次。
  • 我想只对每个值中的一个求和
  • 像总和(75 + 669 + 575 + 423)
    最终输出
  • Like SUM(SUM(sup)+SUM(daily)+SUM(emp)) WHERE dates.pay_date = 'some date'

2 个答案:

答案 0 :(得分:3)

你的意思是这样吗?

SELECT dates.id, 
((SELECT SUM(COALESCE(s.paid_money,0)) FROM supplier_payments WHERE s.payment_date_id = dates.id )+
(SELECT SUM(COALESCE(d.paid_money,0)) FROM daily_payments WHERE d.payment_date_id = dates.id ) +
(SELECT SUM(COALESCE(e.paid_money,0)) FROM emp_payments WHERE e.payment_date_id = dates.id )) as total
FROM payment_dates AS dates

如果这不是您的意思,请提供源表中数据的简要示例(即supplier_payments daily_payments和emp_payments)以及预期结果应该是什么。

<强>更新

我已经构建了一个带有这样的表结构的示例数据库:

CREATE TABLE `daily_payments` (
 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
 `paid_money` decimal(12,2) NOT NULL,
 `payment_date_id` int(11) DEFAULT NULL,
 PRIMARY KEY (`id`)
);

CREATE TABLE `emp_payments` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `paid_money` decimal(12,2) DEFAULT NULL,
  `payment_date_id` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
);

CREATE TABLE `payment_dates` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `pay_date` date NOT NULL,
  PRIMARY KEY (`id`)
);

CREATE TABLE `supplier_payments` (
  `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
  `payment_date_id` int(11) DEFAULT NULL,
  `paid_money` decimal(12,2) NOT NULL,
  PRIMARY KEY (`id`)
);

插入一些随机数据后,此查询似乎会返回您想要的输出:

SELECT id,
COALESCE((SELECT SUM(`daily_payments`.`paid_money`) FROM `daily_payments` WHERE    `payment_date_id`=`payment_dates`.id),0) + 
COALESCE((SELECT SUM(`emp_payments`.`paid_money`) FROM `emp_payments` WHERE `payment_date_id`=`payment_dates`.id),0) + 
COALESCE((SELECT SUM(`supplier_payments`.`paid_money`) FROM `supplier_payments` WHERE `payment_date_id`=`payment_dates`.id),0) as sum_of_payments
FROM payment_dates WHERE pay_date = '2012-09-23'

COALESCE函数是为了避免在一个或多个表中没有找到记录的情况下使用NULL值。

答案 1 :(得分:0)

你是说这个?

SELECT sum(s.paid_money+d.paid_money+e.paid_money)
       FROM payment_dates AS dates
       LEFT JOIN supplier_payments AS s 
                ON dates.id = s.payment_date_id
       LEFT JOIN daily_payments AS d 
                ON dates.id = d.payment_date_id
       LEFT JOIN emp_payments AS e 
                ON dates.id = e.payment_date_id;
       GROUP BY dates.id