如何从我的投票表中获得最大计数投票图片

时间:2016-02-17 11:13:07

标签: mysql laravel count laravel-5 laravel-5.2

SELECT userfbimages_id, count( * )
FROM votes
GROUP BY userfbimages_id 
HAVING count( * ) >= ALL (SELECT count( * )
                          FROM votes
                          GROUP BY userfbimages_id)

我试过这个查询,但我不知道如何在laravel5.2中做到这一点! 我试过了

  

$ topim = Vote :: where(SELECT userfbimages_id,count()FROM投票GROUP   BY userfbimages_id HAVING count()> = ALL(SELECT count(*)FROM votes   GROUP BY userfbimages_id));

返回Null

2 个答案:

答案 0 :(得分:0)

你错过了明星。

count(*)应该是使该查询有效所需的全部内容!

答案 1 :(得分:0)

对于那些为我的问题寻找答案的人,我找到了一个解决方案`

>     return $this->model
>                       ->where('seen','=', 1)
>                       ->with('votes')
>                       -sortby(function($top){
>                       return $top->votes->count();}, SORT_REGULAR, true);

this works with sortby and with a subquery !! but for a pagination you should change this with lefjoin statement :

                return $this->model
                    ->where('seen','=', 1)
                    ->leftJoin('votes','votes.userfbimages_id','=','userfbimages.id')
                    ->selectRaw('userfbimages.*, count(votes.userfbimages_id) AS `count`')
                    ->groupBy('userfbimages.id')
                    ->orderBy('count','DESC')
                    ->paginate($n);
`

我找到了解决方案,所以我希望它对你有所帮助