如何在Laravel 5.6中找到不同用户的最大和值?

时间:2018-12-02 06:22:31

标签: laravel eloquent

此表中有3个“ agentid”。现在我想找到'agentid'的'resolved'的最大和值。并且如果我想使用where条件指定'reportyear'和'reportmonth'那么我该怎么办?我有...

$ currentMonth = date('M');  $ currentYear = date('Y');

如何在laravel中做到这一点?

image of table

1 个答案:

答案 0 :(得分:1)

您想要每个代理ID的已解析总和并获得最高的ID?如果是这样,那么您可以做这样的事情

$higest_resolved=DB::select(DB::raw('SELECT agentid,SUM(resolved) 
as resolved_total FROM table_name GROUP by agentid ORDER by resolved_total DESC LIMIT 1'));

要添加月份和年份条件,您可以按照以下步骤操作

$higest_resolved=DB::select(DB::raw('SELECT agentid,SUM(resolved) 
as resolved_total,reportyear,reportmonth FROM table_name where reportmonth="'.$currentMonth.'" and reportyear="'.$currentYear.'" GROUP by agentid,reportyear,reportmonth ORDER by resolved_total DESC'));

您仍然可以在末尾添加限制1,以仅获得一个值。