在Laravel查询构建器中等效FORCE INDEX FOR JOIN

时间:2016-11-23 22:35:16

标签: php sql laravel-5 laravel-query-builder

我正在构建一个Laravel 5.2应用程序。我有一个本机SQL查询需要转换为查询构建器表单,我的意思是,在Laravel查询构建器中等效于SQL查询。基本上,当我尝试翻译 FORCE INDEX FOR JOIN 命令时,我遇到了一个问题,这是本机SQL查询:

SELECT some columns...
    FROM table1 AS t1 
        LEFT JOIN table2 AS t2 
        FORCE INDEX FOR JOIN (idx_table2) 
            ON ((t1.messageid = t2.messageid) AND (t2.othercolumn = 1))  
        WHERE something... 

所以,到现在为止我已经:

$query= DB::table('table1 as t1')
                        ->leftJoin('table2 as t2', 't1.messageid', '=', 't2.messageid')

那么,我该如何添加这一行FORCE INDEX FOR JOIN (idx_table2)

1 个答案:

答案 0 :(得分:3)

您可以使用以下语法:

$query= DB::table('table1 as t1')
                    ->leftJoin(DB::raw('table2 AS t2 FORCE INDEX FOR JOIN (idx_table2)'), 't1.messageid', '=', 't2.messageid')