如何在Laravel的流利中做子查询?

时间:2013-07-11 11:35:53

标签: php laravel laravel-4

说我有这个SQL:

select * 
from `fluents`
inner join `tests` on `fluents`.`id` = `tests`.`fluent_test_id`
inner join (
    select `fluents`.`id` from `fluents` order by `fluents`.`id` desc limit 10
) as j on `fluents`.`id` = `j`.`id`
order by `fluents`.`created_at`;

我知道我可以运行原始SQL,但作为一项学习练习,我试图将其转换为流利且失败,这甚至可能吗?

1 个答案:

答案 0 :(得分:6)

我在我的项目中这样做了:

DB::table('fluents')->join('tests','tests.fluent_test_id','=','fluents.id')
                    ->join(DB::raw("(select fluents.id from fluents order by `fluents`.`id` desc limit 10) as j"),'j.id','=','fluents.id')
                    ->orderBy('fluents.created_at');

我希望这可以提供一些帮助。