0


I have a SQL query and I want to rewrite it in eloquent syntaxe.
This is my query:

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 there is my attempt to rebuild it with 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');
        });

Or generaly, How can I do a query from another query with Eloquent, like that :

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

There is my full query:

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
Le-Mr-Ruyk
  • 179
  • 2
  • 17
  • I think this still holds true: http://stackoverflow.com/a/24838367/1627227. Also your example doesn't really need a subquery at all, but it might help if you tell us your full query (or problem). You might need to do things different with eloquent/fluent. – martinczerwi Dec 08 '15 at 10:02
  • Yeah - why bother with the subquery!!!! – Strawberry Dec 08 '15 at 10:09
  • Thanks guys for your replies, You can look below the full query that I try to make in Eloquent – Le-Mr-Ruyk Dec 08 '15 at 10:18

0 Answers0