Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/60.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何在db查询生成器中按分组以获取唯一值?_Php_Mysql_Sql_Laravel - Fatal编程技术网

Php 如何在db查询生成器中按分组以获取唯一值?

Php 如何在db查询生成器中按分组以获取唯一值?,php,mysql,sql,laravel,Php,Mysql,Sql,Laravel,有人能帮我把下面的mysql转换成laravel db query builder或eloquent吗?这是我的mysql SELECT max(created_at), jobseeker_id,call_reason,type_of_call FROM calllogs GROUP BY jobseeker_id ORDER BY created_at DESC; 这是我的尝试,但还没有运气 $jobseekers =DB::table('calllogs')

有人能帮我把下面的mysql转换成laravel db query builder或eloquent吗?这是我的mysql

SELECT max(created_at), jobseeker_id,call_reason,type_of_call
FROM calllogs
GROUP BY jobseeker_id
ORDER BY created_at DESC;
这是我的尝试,但还没有运气

             $jobseekers =DB::table('calllogs')
            ->select(DB::raw("max('created_at'),jobseeker_id"))
                 ->groupBy('jobseeker_id')  
                 ->orderBy('created_at','desc')
                 ->get();
                 dd($jobseekers);
请帮帮我

$jobseekers =DB::table('calllogs')
  ->select(DB::raw("max(created_at),jobseeker_id"))
  ->groupBy('jobseeker_id')  
  ->orderBy('created_at','desc')
  ->get();
请尝试使用此代码。 仅删除max中的“”。 它将很好地工作

DB::table(calllogs)
->select(DB::raw('max(created_at) as max_created_at'), 
'jobseeker_id','call_reason','type_of_call')
->groupBy('jobseeker_id')
->orderBy('max_created_at', 'DESC')
->get();   =>As a array.
->first(); => As a object.
请尝试使用此代码。 仅删除max中的“”。 它将很好地工作

DB::table(calllogs)
->select(DB::raw('max(created_at) as max_created_at'), 
'jobseeker_id','call_reason','type_of_call')
->groupBy('jobseeker_id')
->orderBy('max_created_at', 'DESC')
->get();   =>As a array.
->first(); => As a object.
此外,如果您使用的是表联接,请参见下文。 此外,如果您使用的是表联接,请参见下文。
ORDER BY子句不在GROUP BY子句中,它包含未聚合的列“myapp.calllogs.created_at”

这一错误的两个解决方案

1.在config/database.php文件中,将sql严格模式设置为false

或 2.在groupBy子句中添加所有表列

例如:


->groupBy'Jobseker\u id'、'Jobseker\u name'、'Jobseker\u email'

ORDER BY子句不在GROUP BY子句中,它包含未聚合的列“myapp.calllogs.created\u at”

这一错误的两个解决方案

1.在config/database.php文件中,将sql严格模式设置为false

或 2.在groupBy子句中添加所有表列

例如:


->groupBy“求职者id”、“求职者姓名”、“求职者电子邮件”

为什么“还没有好运”?你不够具体。Laravel的查询生成器有一个toSql方法,可以用来调试查询。为什么“还没有运气”?你不够具体。Laravel的查询生成器有一个toSql方法,可以用来调试查询。还没有bro,很抱歉,最近它向我显示了类似ss违例的错误:ORDER BY子句的1055表达式1不在GROUP BY子句中,并且包含未聚合的列“myapp.calllogs.created_at”,它在功能上不依赖GROUP BY子句中的列;这与sql\u mode=only\u full\u group\u by sql不兼容:选择maxcreated\u at,Jobseker\u id from calllogs group by jobseker\u id order by created\u at descIn config/database.php at mysql将:“strict”=>true改为false。天哪,它工作得很好,你能解释一下为什么吗?我真的非常感谢你,我已经试了一个星期了。请看这个。还没有bro,很抱歉迟到了,它向我显示了类似ss违例的错误:ORDER BY子句的1055表达式1不在GROUP BY子句中,并且包含未聚合的列“myapp.calllogs.created_at”,它在功能上不依赖GROUP BY子句中的列;这与sql\u mode=only\u full\u group\u by sql不兼容:选择maxcreated\u at,Jobseker\u id from calllogs group by jobseker\u id order by created\u at descIn config/database.php at mysql将:“strict”=>true改为false。天哪,它工作得很好,你能解释一下为什么吗?我真的非常感谢你,我已经试了一个星期了。请看这个。是的,如果我真的喜欢->groupBy'jobseeker\u id','jobseeker\u name','jobseeker\u email',它不会给我唯一的求职者id,把你的简历发到这里是的,如果我喜欢->groupBy'jobseeker\u id','jobseeker\u name','jobseeker\u email它不会给我唯一的求职者id,把你的简历发到这里