Php “拉雷维尔”;分组方式为;查询问题

Php “拉雷维尔”;分组方式为;查询问题,php,mysql,laravel,eloquent,Php,Mysql,Laravel,Eloquent,请注意,我正在尝试运行一个在原始sql中看起来像这样的查询 SELECT COUNT(cntr) count, address, description FROM resti GROUP BY cntr = HAVING count > 1 在拉雷维尔 我试过这个 DB::table("resti") ->select(DB::raw("COUNT(cntr) count, address, description"))

请注意,我正在尝试运行一个在原始sql中看起来像这样的查询

SELECT COUNT(cntr) count, address,
description FROM resti GROUP BY cntr = HAVING count > 1
在拉雷维尔

我试过这个

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

但是它给出了一些聚合错误。

您的SQL查询应该是这样的

SELECT COUNT(cntr) count, address, description 
FROM resti 
GROUP BY cntr  
HAVING COUNT(cntr) > 1
DB::table("resti")
->select(DB::raw("COUNT(cntr) count, address, description"))
->groupBy("cntr")
->havingRaw("COUNT(cntr) > 1")
->get();
在Laravel中,您的代码应该是这样的

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

请尝试
toSql()
查看最终查询。谢谢。它实际上输出了正确的sql。不确定问题可能来自何方。但是非常感谢。你有什么错误信息?具体在哪里?在删除get()之后,我在查询生成器的末尾添加了一个count,即我添加了->count(),它将select count(*)作为聚合从
resti
groupby
cntr
having count>1打印出来,这会引发未知列错误。当然,
count>1
是未知列。