\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.
Related
So I'm using Laravel eloquents to access data from my database - it's very similar to other statements in my code which work fine, but for some reason this isn't working:
$data->leftjoin('val_keys', function($leftJoin) use (&$key_words)
{
$leftJoin->on('val_keys.val_id', '=', 'vals.id');
})->whereRaw(DB::raw(sprintf('(LOWER(val_keys.key) LIKE "%%%s%%")', strtolower($key_words))));
I keep getting the following error, which shows that there is a group statement that is somehow being added to my SQL query despite the fact that I'm not putting it there:
Integrity constraint violation: 1052 Column 'id' in group statement is ambiguous (SQL: select count(*) as aggregate from `vals` left join `val_keys` on `val_keys`.`val_id` = `vals`.`id` where (`category_id` = 3780) and (LOWER(val_keys.key) LIKE "%hey%") group by `id`)
I've tried adding my own groupBy() to my query, with vals.id as the input, hoping that this would override the group statement that uses id, but it doesn't:
Integrity constraint violation: 1052 Column 'id' in group statement is ambiguous (SQL: select count(*) as aggregate from `vals` left join `val_keys` on `val_keys`.`val_id` = `vals`.`id` where (`category_id` = 3780) and (LOWER(val_keys.key) LIKE "%hey%") group by `vals`.`id`, `id`)
How do I get rid of this group statement so my query will run as intended? I've already tried changing strict to true. I am using Laravel version 5.2.
Thanks
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
Bellow SQL command runs perfectly :
select * from `product` group by `owner_name` order by `id` asc
When I translate above code in my Laravel project to get the same result :
Product::select('*')
->orderBy('id','asc')->groupBy('owner_name')
->get();
This laravel code returns me error that
SQLSTATE[42000]: Syntax error or access violation: 1055
'db1.product.id' isn't in GROUP BY (SQL: select * from product group
by owner_name order by id asc)
Problem is I have many duplicated records with slight differences on some of their columns. I need to get them by owner_name and only one time .
Edit your applications's database config file config/database.php
In mysql array, set strict => false to disable MySQL's strict mode
You have don't need to do 'select(*)', by default it will select all columns data.
Try this:
Product::orderBy('id','asc')->groupBy('owner_name')
->get();
And if you want to fetch selected column you can do like this:
Product::select(['column_1', 'column_2'])
->orderBy('id','asc')->groupBy('owner_name')
->get();
I am trying to do a simple pagination of a query that uses a view:
DB::table('vw_myview')->paginate(50)
And get this error:
SQLSTATE[42000]: Syntax error or access violation: 1140 Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause (SQL: select count(*) as aggregate from vw_myview)
Yet when I manually run the query, it works just fine:
mysql> select count(*) as aggregate from vw_myview;
+-----------+
| aggregate |
+-----------+
| 776 |
+-----------+
1 row in set (0.20 sec)
After searching around I found that I should just have to add a GROUP BY clause (just like the error says) and I have tried doing that with no luck:
DB::table('vw_myview')->groupBy('column_on_view')->paginate(50)
SQLSTATE[42000]: Syntax error or access violation: 1055 'database_name.table_used_by_view.column_on_table' isn't in GROUP BY (SQL: select count(*) as aggregate from vw_myview group by column_on_view)
Okay lets try grouping by a column on one of the tables the view uses:
DB::table('vw_myview')->groupBy('table_used_by_view.column_on_table')->paginate(50)
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'database_name.table_used_by_view.column_on_table' in 'group statement' (SQL: select count(*) as aggregate from vw_myview group by database_name.table_used_by_view.column_on_table)
So why does the query work when I run it manually but fails when run through Eloquent? Any help would be greatly appreciated.
You know, I am thinking your Mysql server has the sql_mode = ONLY_FULL_GROUP_BY turned on... Can you check this... (from mysql ... select ##sql_mode; If it is set, turn it off... and try your query again... I think it shall pass... Mysql 5.7xxx has this turned on by default now...
I've suddenly started getting a weird error in my production environment with count(). The following two statements work totally fine:
User::where('created_at', '2017-06-22 18::33:54')->count()
User::where('created_at', '2017-06-22 18::33:54')->orderBy('last_name')
but if I try
User::where('created_at', '2017-06-22 18::33:54')->orderBy('last_name')->count()
I get the following error:
Syntax error or access violation: 1140 Mixing of GROUP columns (MIN(),MAX(),COUNT(),...) with no GROUP columns is illegal if there is no GROUP BY clause (SQL: select count(*) as aggregate from users where created_at = 2017-06-22 18::33:54 and users.deleted_at is null order by last_name asc)'
This doesn't happen in my local environment, and I'm not sure where to look for an answer in production.
Thoughts?