使用mysql返回字段和所有行的总和

时间:2012-09-23 18:46:18

标签: mysql

我有一张桌子

id      admission       amount          paid_date 

12      0001            1750            2012-09-23
13      0002            1750            2012-09-23
14      0010            1750            2012-09-23

我使用了以下查询

SELECT *, (SELECT SUM(amount) ) 
FROM gains
WHERE  `paid_date`  LIKE '%2012-09-23%'
ORDER BY `paid_date` 

但我的输出是

id      admission       amount          paid_date     (SELECT SUM(amount) ) 

12      0001            1750            2012-09-23          5250

是否可以使用一个查询单独获取所有3条记录和金额总和?

2 个答案:

答案 0 :(得分:3)

SELECT *, 
      (SELECT SUM(amount) 
       FROM gains
       WHERE  `paid_date` LIKE '%2012-09-23%') as sum_amount
FROM gains
WHERE  `paid_date`  LIKE '%2012-09-23%'
group by admission
ORDER BY `paid_date` 

答案 1 :(得分:0)

SELECT *, (SELECT SUM(amount) ) 
FROM gains
WHERE  `paid_date`  LIKE '%2012-09-23%'
GROUP BY admission