使用eloquent从其他Query中选择语句查询

时间:2015-12-08 09:29:23

标签: mysql laravel eloquent query-builder


我有一个SQL查询,我想用雄辩的语法重写它。
这是我的问题:

SELECT study_id FROM (
    SELECT max(studies.end_date), studies.id as study_id
    from  workers
    inner join resumes on workers.id=resumes.worker_id
    inner join studies on resumes.id=studies.resume_id where
    resumes.title="main" group by workers.id) as maste

我试图用Eloquent重建它:

    $qr->select(DB::raw('study_id'))->from(function ($r) {
            $r->selectRaw("max(studies.end_date), studies.id as study_id")
                ->from('workers')
                ->join('resumes', 'resumes.worker_id', '=', 'workers.id')
                ->join('studies', 'studies.resume_id', '=', 'resumes.id')
                ->where('resumes.title', 'main')->groupBy('workers.id');
        });

或者通常,我如何使用Eloquent从另一个查询中进行查询,如下所示:

SELECT id from(Select id,max(date) from b group by id)

我的完整查询:

SELECT count(workers.id) as data ,titlesdiplomas.libelle  as label 
from workers inner join resumes on workers.id=resumes.worker_id
inner join studies on resumes.id=studies.resume_id
inner join diplomas on diplomas.study_id=studies.id
inner join titlesdiplomas on titlesdiplomas.id=diplomas.titlesdiploma_id
where studies.id in(SELECT study_id FROM (
SELECT max(studies.end_date), studies.id as study_id
from  workers
inner join resumes on workers.id=resumes.worker_id
inner join studies on resumes.id=studies.resume_id where
resumes.title="main" group by workers.id) as maste)
and  workers.id in (SELECT worker_id from career_histories where current=1 '
. $whereQuery . ' ) group by titlesdiplomas.libelle

0 个答案:

没有答案