具有联合的Laravel查询无法分组

时间:2019-09-20 10:27:55

标签: php mysql laravel-5.8

我正在尝试对评论进行排序。每个评论都可以有一个parent_id,以防重播到另一个评论。

我需要在parent_date和comment_date之前订购所有评论。

我创建了以下查询:

$union = DB::table("press_comments as c2")
        ->select("c2.body", "c2.id", 'c2.parent_id', 'c2.created_at as ac', 'p.created_at as ap')
        ->join('press_comments as p', 'c2.parent_id', '=', 'p.id');


$comments = DB::table('press_comments as c1')
        ->select("c1.body", "c1.id", 'c1.parent_id', 'c1.created_at as ac', 'c1.created_at as ap')
       // ->where("parent_id", "=", 0)
        ->union($union)
        ->orderBy("ap","asc")
        ->orderBy( "ac","asc")
        ->groupBy('id')
        ->paginate();

但是我的台词太多了:groupBy似乎无法正常工作。

在普通的mysql中,它起作用:

SELECT *
   FROM (   SELECT c1.id,
              c1.parent_id,
              c1.created_at ac,
              c1.created_at ap
       FROM press_comments c1
       UNION
       SELECT c2.id,
              c2.parent_id,
              c2.created_at ac,
              p.created_at ap
       FROM press_comments c2
       JOIN press_comments p
       ON c2.parent_id = p.id) c
GROUP BY id
ORDER BY c.ap, c.ac;

Laravel查询中缺少什么?

0 个答案:

没有答案