2014-11-05 48 views
3

請我試圖運行一個查詢,看起來像這樣的原始SQLLaravel「組具有」查詢發出

SELECT COUNT(cntr) count, address, 
description FROM resti GROUP BY cntr = HAVING count > 1 

在laravel。

我已經試過這

DB::table("resti") 
       ->select(DB::raw("COUNT(cntr) count, address, description")) 
       ->groupBy("cntr") 
       ->havingRaw("count > 1") 
       ->get(); 

但它給一些總誤差的。

+2

嘗試'toSql()'看到最終的查詢。 – Cheery 2014-11-05 00:38:58

+0

謝謝。它實際上輸出正確的SQL。不確定問題可能來自哪裏。但非常感謝。 – Cozzbie 2014-11-05 00:45:41

+0

你有什麼錯誤信息?究竟在哪裏? – Cheery 2014-11-05 00:47:10

回答

5

你的SQL查詢應該是這樣的

SELECT COUNT(cntr) count, address, description 
FROM resti 
GROUP BY cntr 
HAVING COUNT(cntr) > 1 

在Laravel你的代碼應該是這樣的

DB::table("resti") 
->select(DB::raw("COUNT(cntr) count, address, description")) 
->groupBy("cntr") 
->havingRaw("COUNT(cntr) > 1") 
->get();