Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel "group by having" query issues

Please I am trying to run a query that looks like this in raw sql

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

in laravel.

I have tried this

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

But it gives of some aggregate error.

like image 733
Cozzbie Avatar asked Nov 05 '14 00:11

Cozzbie


1 Answers

Your SQL query should be like this

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

In Laravel your code should be like this

DB::table("resti")
->select(DB::raw("COUNT(cntr) count, address, description"))
->groupBy("cntr")
->havingRaw("COUNT(cntr) > 1")
->get();
like image 162
Belal mazlom Avatar answered Oct 05 '22 23:10

Belal mazlom