简体   繁体   中英

How to convert this MySQL query to a equivalent Laravel 5.4 Elequent/query builder query?

SELECT `id` FROM `jobs` WHERE `job_type` IN ( 'topemployercheckbox1','topemployercheckbox2') OR `job_category` IN ( 'Contract','Intern') ;

我需要将此MySQL查询转换为laravel 5.4查询生成器查询。

you can do

ModelName::whereIn('job_type',['topemployercheckbox1','topemployercheckbox2'])
    ->orwhere(function($q){
          $q->whereIn('job_category',['Contract','Intern'])
})->get('id');

or

ModelName::whereIn('job_type',['topemployercheckbox1','topemployercheckbox2'])
    ->whereIn('job_category',['Contract','Intern'],'or')->get('id');

You can use union of two whereIn query.

$query1= \DB::table("jobs")select("id")->whereIn("job_type",['topemployercheckbox1','topemployercheckbox2']);
$query2=\DB::table("jobs")select("id")->whereIn("job_category",['Contract','Intern']);
$job_ids= $query1->union($query2)->get();

Your query would be like as below. Here we have considered your model name is as Job.

 Job::select('id')
    ->whereIn('job_type',['topemployercheckbox1','topemployercheckbox2'])
    ->orWhereIn('job_category',['Contract','Intern'])
    ->get();

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