如何在 laravel 上编写查询 sql
How to write query sql on laravel
我是 Laravel 4 的新手。我有 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
我不知道如何在 Laravel 4 上写这个?
我的做法是:
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();
不确定是否有更快或更有效的方法。
我还没有检查过,但你可以试试这个
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
我是 Laravel 4 的新手。我有 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
我不知道如何在 Laravel 4 上写这个?
我的做法是:
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();
不确定是否有更快或更有效的方法。
我还没有检查过,但你可以试试这个
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