gpt4 book ai didi

mysql - GROUP BY 在 SELECT 中选择

转载 作者:行者123 更新时间:2023-11-29 07:40:38 26 4
gpt4 key购买 nike

我有一个表,我想选择一些行并按日期对它们进行分组。问题是当我在选择中选择该行时,该行似乎未按日期分组,请参阅示例:

SELECT date, SUM(price), (SELECT SUM(price), date FROM expense WHERE 
user = 'Catherine') as cprice FROM expense GROUP BY date

它不会对cprice进行分组,它会将所有Catherine价格相加,并且不会按日期进行分组,顺便说一句,我尝试将GROUP BY date放入选择范围内,但也不起作用。有什么建议吗?

expense table:
id user price date
1 Frank 20 2014
2 Catherine 10 2013
3 Catherine 20 2014

谢谢

最佳答案

你可以试试这个:

SELECT 
date,
SUM(price) as totalPrice,
SUM(IF(user='Catherine', price, 0)) as cprice
FROM expense
GROUP BY date

如果您想要两个价格在一起,那么您可以尝试以下操作:

SELECT 
date,
SUM(price) + SUM(IF(user='Catherine', price, 0)) as cprice
FROM expense
GROUP BY date

关于mysql - GROUP BY 在 SELECT 中选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29187726/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com