如何在laravel上编写查询sql

时间:2015-12-27 06:10:31

标签: php mysql laravel laravel-4

我是Laravel 4的新手。我有像这样的查询Sql

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

我不知道如何在Laravel 4上写这个?

2 个答案:

答案 0 :(得分:1)

我还没有检查过,但你可以试试这个

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

此处有更多详情https://laravel.com/docs/4.2/queries

答案 1 :(得分:0)

我这样做的方式是:

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

不确定是否有更快或更有效的方式。