我正在使用此查询获取单个总值:
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
一起运行单行?
感谢您的帮助
答案 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
即可。