【问题标题】:How can I do subqueries in Laravel's fluent?如何在 Laravel 的 fluent 中进行子查询?
【发布时间】:2013-07-09 15:56:48
【问题描述】:

假设我有这段 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,但作为一项学习练习,我正试图将其转换为流利的并且失败得很惨,这甚至可能吗?

【问题讨论】:

  • 使用 DB::query($sql) for laravel fluent 来执行查询。
  • 我的意思是不使用查询生成器,而不仅仅是简单地执行原始 SQL。
  • ok 你用的是 laravel 3 还是 4?

标签: php laravel laravel-4


【解决方案1】:

我在我的项目中就是这样做的:

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

我希望这能有所帮助。

【讨论】:

    猜你喜欢
    • 2013-01-05
    • 2020-01-02
    • 1970-01-01
    • 2021-02-13
    • 2013-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-23
    相关资源
    最近更新 更多