具有相同列值的行的总和

我想获得具有相同值的多列的总值。


前任。我想总结第 1 个月、第 2 个月、第 3 个月的所有 Angry 反应 .... 等等,但我只想要每个月的总数


MONTH    | ANGRY | DISGUST | HAPPY |......|PEOPLE

0        | 99    | 87      | 92    |      | 250

1        | 200   | 45      | 12    |      | 400

.

.

.

11       |....................................|


交互式爱情
浏览 100回答 2
2回答

墨色风雨

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

守着星空守着你

利用groupBy  $data = DB::table('your_table_name')->select(            DB::raw('sum(angry) as sums'))  ->groupBy('months')  ->get();
打开App,查看更多内容
随时随地看视频慕课网APP