Laravel“按拥有分组”查询问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26747740/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:22:35  来源:igfitidea点击:

Laravel "group by having" query issues

phpmysqllaraveleloquent

提问by Cozzbie

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

请我尝试在原始 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.

但它给出了一些总体错误。

回答by Belal mazlom

Your SQL query should be like this

你的 SQL 查询应该是这样的

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

In Laravel your code should be like this

在 Laravel 中你的代码应该是这样的

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