如何将mysql查询转换为laravel查询生成器

时间:2015-11-20 09:05:56

标签: php mysql sql laravel-5 query-builder

有没有办法将以下查询转换为laravel查询构建器?

select `employee_id`
from `otc_employee_qualifications` 
where `emp_qualifctn_type` IN ('29','27') 
group by `employee_id`
having count(Distinct `emp_qualifctn_type`) = 2

enter image description here

2 个答案:

答案 0 :(得分:2)

尝试如下:

$users = DB::table('otc_employee_qualifications')
         ->select('employee_id')
         ->whereIn('emp_qualifctn_type', [27,29])
         ->groupBy('employee_id')
         ->having(DB::raw("count(Distinct emp_qualifctn_type)"), '=', 2)
         ->get();

答案 1 :(得分:-1)

答案:

  DB::select('employee_id')
        ->from('otc_employee_qualifications')
        ->whereIn('emp_qualifctn_type', ('29', '27'))
        ->groupBy('employee_id')
        ->having(DB::raw('count(Distinct emp_qualifctn_type)'), '=', 2)
        ->get();

您可以使用下面的网站将SQL查询转换为laravel eloquent查询。

这会将SQL查询转换为laravel eloquent base query

Convert SQL query to Eloquent base query

相关问题