如何在Laravel 5.4中进行此查询?

时间:2018-03-28 07:56:41

标签: sql laravel laravel-5

查询是:

SELECT * FROM `noticias` WHERE `fecha` in ( select `fecha` from `noticias` group by `fecha` having count(*) > 1)

我正在尝试这种形式:

DB::table('noticias')->groupBy('fecha')->having('fecha', '>', 1)->get();

但是这给了我错误: SQLSTATE [42000]:语法错误或访问冲突:1055'conversiones.noticias.id'不在GROUP BY中(SQL:select {from noticias group by fecha fecha }}> 1)

谢谢!

1 个答案:

答案 0 :(得分:3)

您的查询将转换为查询构建器,如下所示:

DB::table("noticias")
   ->whereIn('fecha', function ($q) {
        return $q->from('noticias')->select("fecha")
                 ->groupBy("fecha")
                 ->having(DB::raw("count(*)"), ">", 1);
   })->get();