代码之家  ›  专栏  ›  技术社区  ›  Dewagg

按总和排序的Laravel查询

  •  2
  • Dewagg  · 技术社区  · 6 年前

    抱歉我英语不好。我有桌子: recent_posters

    我想每天展示十大海报。我试过的东西:

    RecentPosters::select('id', 'user', DB::raw('SUM(id) as daily_posts'))
        ->groupBy('id')
        ->orderByRaw('SUM(id) DESC')
        ->limit(10)
        ->get();
    

    但这给了我一个具有相同用户的数组,我如何显示10个不同的用户?事先谢谢。

    2 回复  |  直到 6 年前
        1
  •  1
  •   Saurabh Mistry    6 年前

        $today_top_10= RecentPosters::whereRaw('Date(created_at) = CURDATE()')
                                      ->distinct('user')
                                      ->orderBy('created_at','desc')
                                      ->take(10)
                                      ->get();