sql - How to select a field based on the maximum number of rows -
i'm using laravel 4.2 , have following table looks
id|chosen(boolean('0','1'))|subject|user_id 1|1 |cricket|2 2|0 |cricket|3 3|1 |rugby |2
i choose user_id
has chosen rows or in other words user has 1s in chosen column. in case user 2 correct result.
you can using query builder
$table = db::table('table') ->select(array(db::raw('count(user_id) countuser'))) ->where('chosen', 1) ->orderby('countuser', 'desc') ->groupby('user_id') ->first();
Comments
Post a Comment