如何显示由最大行数确定的用户 [英] How to display a user determined by the maximum number rows

查看:130
本文介绍了如何显示由最大行数确定的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是laravel 4.2,我有下面的表格,看起来像这样

I'm using laravel 4.2 and I have the following table that looks like this

ID|chosen(boolean('0','1'))|subject|user_id
 1|1                       |cricket|2
 2|0                       |cricket|3
 3|1                       |rugby  |2

我想选择具有最多选择行的user_id,换句话说,他们的最多1位用户选择列。所以在这种情况下,用户2将是正确的结果。

I would like to choose the user_id that has the most chosen rows or in other words the user that has the most 1s in their chosen column. So in this case user 2 would be the correct result.

我相信这是答案

$table = DB::table('tablename')
         ->select(array(DB::raw('count(user_id) AS CountUser')))
          ->where('chosen', 1)
         ->orderBy('CountUser', 'desc')
         ->groupBy('user_id')
         ->first();

然而如何在我的视图中显示user_id?例如:

{{$table->user_id}} //should give me '2'

错误消息:

未定义的属性:stdClass :: $ user_id


Undefined property: stdClass::$user_id

推荐答案

您只选择计数。尝试这个

You are selecting the count only. Try this

$table = DB::table('tablename')
     ->select(DB::raw('count(user_id) AS CountUser, user_id'))
      ->where('chosen', 1)
     ->orderBy('CountUser', 'desc')
     ->groupBy('user_id')
     ->first();

这篇关于如何显示由最大行数确定的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆