sql sum和group by给出多行

时间:2013-04-29 14:08:46

标签: mysql sql

我正在使用此查询获取单个总值:

SELECT SUM(`contact_awards`.`amount`) AS amount
FROM (`contact_awards`)
WHERE
    `contact_awards`.`gift_award_id` IN (1) 
AND `created` BETWEEN '2013-05-05' AND '2014-04-01'
GROUP
    BY `contact_awards`.`amount`

但是,在运行时,它会生成包含amount字段的两行:

500
1000

为什么它与sum 1500一起运行单行?

感谢您的帮助

2 个答案:

答案 0 :(得分:3)

删除GROUP BY子句

SELECT SUM(contact_awards.amount) AS amount
FROM   contact_awards
WHERE  contact_awards.gift_award_id IN (1) 
       AND created BETWEEN '2013-05-05' AND '2014-04-01'

您获得两条记录的原因是因为contact_awards.amount有两个不同的值被分组。

答案 1 :(得分:2)

当您使用group by时,结果将由您的分组子句分隔。如果您想要总结一行,只需删除查询末尾的group by即可。