I have a table with fields:
|| id || name || c_number ||
I have a query:
$query ->select('c_number', DB::raw('count(*) as total'))
->groupBy('c_number')
->get();
output:
[{"c_number":1,"total":4},{"c_number":2,"total":2},{"c_number":3,"total":2},{"c_number":4,"total":2}]
I need select from output only value(-s) of c_number where total is max.
Can you tell me, please, how to do that with query?
You could using a mixture or orderBy() and first().
->select('c_number', DB::raw('count(*) as total'))
->groupBy('c_number')
->orderBy('total', 'desc')
->first();
Hope this helps!
Related
How can I write a query that will get the first active game_id that does not occur twice in the table below?
How about :
DB::table('tablename')
->select(DB::raw('COUNT(game_id) as totalgames, game_id'))
->where('is_active', 1)
->groupBy('game_id')
->havingRaw('COUNT(game_id) = 1')
->first();
This will only show all the game id and the number of their occurences
$user_info = DB::table('usermetas')
->select('browser', DB::raw('count(*) as total'))
->groupBy('browser')
->get();
I want to build a query.
here is table:
id user_id sn_no
1 22 00112233
2 22 987654325
3 22 65489732
4 25 984123123
5 25 9568456456
6 25 65456456
I want result like this:
{
"user_id":22,
"sn_no": "00112233,987654325,65489732"
}
{
"user_id":25,
"sn_no": "984123123,9568456456,65456456"
}
Can anyone please help to solve this issue?
I have tried: concat, GROUP_CONCAT but can not get the result.
Can you please help me add this in Join query?
$users = \DB::table('users')
->join('users_mcu', 'users.id', '=', 'users_mcu.user_id')
->join('country_user', 'users.id', '=', 'country_user.user_id')
->join('country_phase_color', 'country_user.country_id', '=', 'country_phase_color.id')
->select('users.id', 'users.first_name', 'users.last_name', 'users.company', 'users.designation', 'users.lang', 'users.phone', 'users.disp_graph', 'users.user_image', 'users.email', 'users.role', 'users.created_at', 'users.updated_at', 'country_user.country_id', 'country_phase_color.country_name')
->get();
Try this-
YourModel::select('user_id')
->selectRaw('GROUP_CONCAT(sn_no) as sn')
->groupBy('user_id')
->get();
Try this.
$data = \DB::table('tablename')->select('user_id', \DB::raw('group_concat(sn_no) as sn_nos'))->groupBy('user_id')->get();
as if you want to change SEPARATOR then you can use SEPARATOR for it otherwise the default will be ','
\DB::table('tablename')->select('user_id',DB::raw("(GROUP_CONCAT(sn_no SEPARATOR '#')) as `sn_nos`"))->groupBy('user_id')->get();
use mysql group_concat function and laravel groupBy and raw function
DB::tabe('tableName')
->select('id','user_id',\DB::raw('GROUP_CONCAT(`sn_no`) as sn_no_t'))
->groupBy('user_id')
->get()
Try this
use DB;
DB::table('tableName')
->select('id','user_id','DB::raw("(GROUP_CONCAT(sn_no SEPARATOR ',')) as `sn_nos`")')
->groupBy('user_id')
->get();
You can try this
Model::select('user_id')
->selectRaw('GROUP_CONCAT(sn_nos) as sn_nos')
->groupBy('user_id')
->get();
This is my Php code
$elements= DB::table('transactions')
->select('product_id', DB::raw('count(*) as total'))
->groupBy('product_id')
->get();
I want to apply an orderby and a limit('5') on count
Any orientation ? Thank you.
add orderBy() ..and take()
$elements= DB::table('transactions')
->select('product_id', DB::raw('count(*) as total'))
->groupBy('product_id')
->orderBy('your_column')
->take(5)
->get();
I have this SQL query:
SELECT *, count(*) as mostView FROM users RIGHT JOIN visitors ON users.id = visitors.user_id GROUP BY users.id HAVING users.isActive=1 AND users.fullName IS NOT NULL AND users.photo_id IS NOT NULL AND users.role_id IN(1, 3) ORDER BY mostView DESC LIMIT 5
It works, but I need convert to Laravel eloquent, i'm using laravel 5.6, could any one helpe me thanks in advance
I'm assuming that you have map the relationship in your Model if you do you can do like below,
$userViews = User::->with('vistors')
->where('isActive',1)
->whereNotNull('fullName')
->whereNotNull('photo_id')
->whereIn('role_id ',[1,3])
->get();
$mostView = $userViews->sortBy(function ($collection) {
return $collection->vistors->count()
})->take(5)
hope this helps
Not going to spoon feeding but can provide you some ideas to convert raw sql to eloquent, your sql should ideally be like this:
User::selectRaw('*', 'count(*) as mostView')
->rightJoin()
->groupBy()
->having()
->where // whereNotNull // wherIn
->orderBy()
->take(5)
->get();
Kindly refer to laravel docs: https://laravel.com/docs/5.6/queries
Thanks for all i'm studies Database: Query Builder on the link
https://laravel.com/docs/5.6/queries
and so i solved my question by myself
the answer is:
$mostViews = DB::table('users')
->rightJoin('visitors', 'users.id' , '=' , 'visitors.user_id')
->select('users.*', DB::raw('count(*) as mostView'))
->where('isActive', '=', '1')
->whereNotNull('fullName')
->where('photo_id', '<>', 'NULL')
->where(function ($q){
$q->where('role_id', '=', '1')
->orWhere('role_id', '=', '3');
})
->groupBy('user_id')
->orderBy('mostView', 'desc')->take(5)->get();
I want to run this mysql query in Laravel 5 using the DB query :
// SELECT *, rating/number as total FROM `courses` order by total DESC;
This is what I tried :
$query = \DB::table('courses')->select('*');
$courses = $query->addSelect('rating/number as total')
->orderBY('total DESC')
->get();
but, rating/number is considered as a table column . The same thing happens when I tried it inside parenthesis (rating/number).
Any help?
$courses = \DB::table('courses')
->selectRaw('*, rating/number as total')
->orderBY('total', 'DESC')
->get();
Can you use Raw Expressions for it? Maybe something like this:
$courses = \DB::table('courses')
->select(DB::raw('*, (rating / number) as total'))
->orderBy('total DESC')
->get();