2013-07-11 30 views
-1

說我有此位的SQL:如何在Laravel的流利中進行子查詢?

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但作爲一個學習的過程,我試圖將其轉換爲流暢慘遭失敗,這甚至可能?

+0

使用DB :: query($ sql)爲laravel流利執行查詢。 –

+0

我的意思是不使用查詢生成器,而不僅僅是簡單地執行原始SQL。 – rich97

+0

好的你使用laravel 3還是4? –

回答

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

我希望這能有一定的幫助。

相關問題