简体   繁体   中英

How to convert mysql query to laravel query builder

Is there any way to convert following query to laravel query builder?

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

在此处输入图片说明

Try as below :

$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();

Answer:

  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();

You can convert SQL query into laravel eloquent query by using bellow website.

This will convert SQL query to laravel eloquent base query

Convert SQL query to Eloquent base query

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM