b4dQuetions
b4dQuetions

Reputation: 1619

How to write query sql on laravel

I'm new to Laravel 4. I have Query Sql Like this

SELECT at.test_id, MAX(at.result), COUNT(status_assessment_id='2') FROM recruitment_process a LEFT JOIN recruitment_result at ON a.id = at.recruitment_id GROUP BY test_id

I do not know how can I write this on Laravel 4?

Upvotes: 2

Views: 97

Answers (2)

ARIF MAHMUD RANA
ARIF MAHMUD RANA

Reputation: 5166

I haven't checked but you can try this

DB::table('recruitment_process a')
    ->select('at.test_id', DB::raw('MAX(at.result)'), DB::raw('COUNT(status_assessment_id="2")'))
    ->leftJoin('recruitment_result at', 'a.id', '=', 'at.recruitment_id')
    ->groupBy('at.test_id')
    ->get();

More details here https://laravel.com/docs/4.2/queries

Upvotes: 2

Matt
Matt

Reputation: 2869

The way I would do it would be:

DB::table('recruitment_process')
    ->select(
    array('recruitment_result.test_id', 
           DB::raw('COUNT(status_assessment_id='2') as mycount'), 
           DB::raw('MAX(recruitment_result.result) as maxvalue')))
    ->left_join('recruitment_result', 'recruitment_process.id', '=', 'recruitment_result.recruitment_id')
    ->group_by('test_id')
    ->get();

Not sure if there is any faster or more effective way.

Upvotes: 0

Related Questions