2

我有一张桌子

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 条记录和金额的总和?

4

2 回答 2

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` 
于 2012-09-23T18:56:24.130 回答
0
SELECT *, (SELECT SUM(amount) ) 
FROM gains
WHERE  `paid_date`  LIKE '%2012-09-23%'
GROUP BY admission 
于 2012-09-23T19:01:42.287 回答