2016-11-15 68 views
0

請幫幫我。Postgres函數WITH在Laravel QueryBuilder中

我已經得到了原始的SQL

WITH collections AS (
    SELECT 
     ... 
    FROM 
     ... 
) 
SELECT 
    ... 
FROM collections c1 
LEFT JOIN collections c2 
    ... 

我如何使用Laravel的QueryBuilder寫呢?

+1

Laravel QueryBuilder不支持'WITH'語法。你有2個選項:重寫查詢而不用'WITH'或使用接受原始sql查詢的DB :: select()。 – AlexM

回答

0

您可以使用Laravel的查詢生成器的RAW方法(https://laravel.com/docs/5.3/queries#raw-expressions)。只需謹慎處理潛在的SQL注入漏洞。

+0

現在我正在這樣做: $ queryResult = DB :: select(「 with collections AS( SELECT .....))」; –

+0

您可以發佈您提交的整個查詢語句嗎? – Ronald

+0

'$ QueryResult中= DB ::選擇(」 與集合AS( SELECT tcm.shop_id,... FROM template_collection_menu AS中醫 LEFT JOIN template_collection AS TC ON tc.id = tcm.template_collection_id WHERE tcm.shop_id IN (:mainShopId,:currentShopId) ORDER BY tcm.position ASC ) SELECT COALESCE(c2.shop_id,c1.shop_id)作爲shop_id,... FROM集合的c1 LEFT JOIN集合C2 ON c2.template_collection_id = C1 .template_collection_id AND c1.shop_id =:mainShopId AND c2.shop_id =:currentShopId WHERE c1.shop_id =:mainShopId 「,['mainShopId'=> $ mainShopId,...]) ;' –