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?
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
\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.
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 have the following query:
select
count(*) as aggregate
from
`images`
where
`images`.`imageable_id` = ?
and `images`.`imageable_id` is not null
and `images`.`imageable_type` = ?
and `generated` = ?
order by
`order` asc
When I run this in the MySQL REPL it works perfectly. However when I run it in PHP with PDO, I get the following 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
I'm running PHP version 7.0.23-1+ubuntu14.04.1+deb.sury.org+1
Does anyone have a hint on how to diagnose and cure this issue?
For future reference, when two connections to the same server are behaving differently, check the mode.
It turns out PDO was running in strict mode and the MySQL REPL was not.
I've become extremely confused I'm trying to run a query through laravels query builder which should work but it's throwing odd errors.
I'm trying to count records from the tips table where the status is 'Won' between a daterange and then join the users table on the user_id to retrieve the users username.
This is my query builder
$topfiveTipsters = DB::table('tips')
->select(DB::raw('count(status) as wincount, users.name'))
->join('users', 'users.id', '=', 'tips.user_id')
->whereBetween('tips.created_at',[$start,$end])
->where('status','Won')
->groupBy('users.id')
->orderBy('wincount', 'desc')
->get();
However it's throwing the error
QLSTATE[42000]: Syntax error or access violation: 1055 'digthetip.users.name' isn't in GROUP BY (SQL: select count(status) as wincount, users.name from tips inner join users on users.id = tips.user_id where tips.created_at between 2016-11-01 00:00:00 and 2016-11-02 10:39:02 and status = Won group by users.id order by wincount desc)
but if I run the exact query the error has outputted into the SQL console within PHPMyAdmin the query runs fine and returns the results I require.
Am I missing something? I'm relatively new to Laravel and I'm massively confused how it's still throwing an error.
Ok so it turns out you can turn of SQL strict mode in /app/database.php which will prevent the error from happening.
'strict' => false,