This question already has answers here:
Error related to only_full_group_by when executing a query in MySql
(18 answers)
Closed 4 years ago.
I want to get all messages for auth user in his inbox with name of people who send him messages.
I have query
$user_id=Auth::id();
$messages=Chat::with('user')
->where('user_id',$user_id)
->groupBy('friend_id')
->limit(10)
->get();
But Laravel won't me to get that and said:
SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'mojaodeca.chats.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (SQL: select * from chats where user_id = 1 group by friend_id limit 10)
use this
$user_id=Auth::id();
$messages=Chat::with('user',function($q){
$q->groupBy('friend_id')
})
->where('user_id',$user_id)
->limit(10)
->get();
Please check MySQL Handling of GROUP BY - Your select list must be in your aggregate functions.
$messages = Chat::select('user_id', DB::raw('COUNT(user_id) as count'))
->with('user')
->groupBy('friend_id')
->limit(10)
->get();
Related
i get this error when i upload it on online cpanel
Syntax error or access violation: 1055 Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'dreamsch_school_management.students.id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (SQL: select class, count(*) as total from students group by class order by id asc)
in controller i Used
$groups = DB::table('students')
->select('class', DB::raw('count(*) as total'))
->groupBy('class')
->orderBy('id', 'ASC')
->get();
i want to arrange in assending order by class. in bar chart. but it give error in order by line but correctly runs in my localhost. can you please help me with the query.
It was my expectation
but without ordering i get this one
Please see the labels in the pictures then you can get the errors. now I want to know how can I place ordering by ID or class when I upload my project in cpanel.
you have to disabled strict mode in your mysql server
just put 'strict' => false in your config/database in the server
I have a syntax error in my query builder. When running on Windows 10 it works, but on Ubuntu it won't work. I think problem is with created/modified db fields.
Here is the code:
return $this->getMyRepository()
->createQueryBuilder('a')
->select('a, MIN(a.numbers) AS min_no')
->where('a.item = :item')
->setParameter('item', $itemId)
->getQuery()
->getResult();
which results in the error:
Syntax error or access violation: 1055 Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'bs.a0_.created' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by
How can I overcome this issue?
This question already has answers here:
Laravel pagination not working with group by clause
(5 answers)
Closed 3 years ago.
I want to make paginate from laravel, group by year and month only
when i used get , it work well
$data = DB::table('gallery')
->select(DB::raw('DATE_FORMAT(created_at, "%Y-%m") as tanggal,count(created_at) as jumlah'),'gallery.*')
->where('type','Foto')
->groupBy('tanggal')
->orderBy('tanggal','desc')
->get();
but when i use paginate , there is an error :
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'tanggal' in 'group statement' (SQL: select count(*) as aggregate from gallery where type = Foto group by tanggal)
$data = DB::table('gallery')
->select(DB::raw('DATE_FORMAT(created_at, "%Y-%m") as tanggal,count(created_at) as jumlah'),'gallery.*')
->where('type','Foto')
->groupBy('tanggal')
->orderBy('tanggal','desc')
->paginate(2);
Or any idea for group by just year and month laravel 5.4?
From https://laravel.com/docs/5.4/pagination
Currently, pagination operations that use a groupBy statement cannot be executed efficiently by Laravel. If you need to use a groupBy with a paginated result set, it is recommended that you query the database and create a paginator manually.
Here's an alternative solution provided on Jen's Segers's blog
This question already has answers here:
Group by not working - Laravel
(8 answers)
Closed 3 years ago.
I'm trying to use order by without adding the order column in groupby, it only works if I execute it directly from the database but from laravel I get database error
I made this eloquent code
Comment::select('product_id')->where('shop_name', $shop)->groupby('product_id')->distinct()->orderBy('created_at')->paginate(12)
it will product the following query
select distinct DISTINCT(product_id) from comments where shop_name
= 'shopname' group by product_id order by created_at asc limit 12 offset 0
if I rub the above query directly in database it works
but if I use Laravel eloquent code it fires this error
SQLSTATE[42000]: Syntax error or access violation: 1055
'areviews_areviewzappz.comments.created_at' isn't in GROUP BY (SQL:
select distinct DISTINCT(product_id) from comments where shop_name
= 'shopname' group by product_id order by created_at asc limit 12 offset 0)
how can I solve this issue ?
The issues is that you SHOULD really include the ORDER BY in the GROUP BY list as this is best practise.
The reason it works when you are on the the sql mode set to ''. However, Laravel by default (I think) has Strict as TRUE,
You have 2 options:
Add the created_at to the GROUP BY clause (Recommended)
Change the strict mode to false
A bit more info on the 2nd option:
How can I solve incompatible with sql_mode=only_full_group_by in laravel eloquent?
You can group it after you have retrieved the results rather than in the MySQL query - so you group the collection after it has been retrieved, not the table entries in the query itself.
Comment::select('product_id')
->where('shop_name', $shop)
->distinct()
->orderBy('created_at')
->paginate(12)
->groupBy('product_id')
Laravel collection groupBy method: https://laravel.com/docs/5.7/collections#method-groupby
Related post: Laravel Query Buider Group By Not Getting All The Records
\App\Service::groupBy('type')->orderBy('id', 'DESC')->get();
I am using laravel 5.5..
Can help me for resolve this problem..?
Error report
"SQLSTATE[42000]: Syntax error or access violation: 1055 'db_name.tbl_name.id' isn't in GROUP BY (SQL: select * from tbl_name group by type order by id desc) "
If you got this error
SQLSTATE[42000]: Syntax error or access violation: 1055 'tablename.fieldname' isn't in GROUP BY (SQL: select * from `tablename` group by `fieldname`)
Then edit your database config file config/database.php
In mysql array, set strict => false to disable MySQL's strict mode
When grouping in MySQL you can only work with either fields you are grouping by or fields used in aggregate functions of your select. Therefore orderBy('id', 'DESC') will do nothing in your case.