我想获得具有相同值的多列的总值。前任。我想总结第 1 个月、第 2 个月、第 3 个月的所有 Angry 反应 .... 等等,但我只想要每个月的总数MONTH | ANGRY | DISGUST | HAPPY |......|PEOPLE0 | 99 | 87 | 92 | | 2501 | 200 | 45 | 12 | | 400...11 |....................................|
2 回答

墨色风雨
TA贡献1853条经验 获得超6个赞
您似乎正在寻找简单的聚合:
select
month,
sum(angry) angry,
sum(disgust) disgust,
sum(happy) happy,
...
sum(people) people
from mytable
group by month
此外,正如草莓所评论的那样,您可能应该考虑修复您的架构。您可以只有三个列,而不是一堆列:
date -- a legitimate datetime column
mood -- angry, disgust, ...
value -- an integer value
有了这个设置,您就可以生成一个带有如下查询的报告:
select year(date), month(date), mood, sum(value)
from mytable
group by year(date), month(date), mood

守着星空守着你
TA贡献1799条经验 获得超8个赞
利用groupBy
$data = DB::table('your_table_name')->select(
DB::raw('sum(angry) as sums'))
->groupBy('months')
->get();
- 2 回答
- 0 关注
- 99 浏览
添加回答
举报
0/150
提交
取消