I am facing this error for the past 2-3 days. I hold this task to check this later in my free time. But still, I am getting the same error. Please help me to find the error. I am tired to check all solutions on google. But nothing works.
Error
Illuminate\Database\QueryException: SQLSTATE[42000]: Syntax error or access violation: 1055 'dating-app.messages.id' isn't in GROUP BY (SQL: select * from messages group by room_id having is_read = 0) in file C:\xampp\htdocs\dating-app\vendor\laravel\framework\src\Illuminate\Database\Connection.php on line 692
Here is my query
$messages = Message::groupBy('room_id')->having('is_read', 0)->get();
return response()->json(['status' => true, 'message' => $messages]);`
Any solution appreciated!
That's because any column you use in a select statement must appear in the group by clause. So select * and group by don't get along. Remove groupBy('room_id') in your relationship and see how that goes.
Alternatively, disable MySQL strict mode by going into config/database.php and setting strict => false for MySQL, if you are using MySQL version >5.7.
source
Related
I am working on a Laravel API where I am using Eloquent to access a MySQL database with a couple of joins and a group by clause.
When Eloquent runs I get an error, but the error outputs the built SQL which when I run manually on the database, it works fine, so I'm a little confused what the problem might be.
The Eloquent query builder is as follows:
$issues = \DB::table('crash_groups')
->join('projects', 'projects.project_id', '=', 'crash_groups.project_id')
->join('crash_info', 'crash_info.crash_group_id', '=', 'crash_groups.crash_group_id')
->where('projects.organisation_id', $organisation_id)
->where('crash_info.created_at', '>', $time_interval_sql)
->groupBy('crash_info.crash_group_id')
->get();
When the above is executed I get the following error outputted
SQLSTATE[42000]: Syntax error or access violation: 1055
'crash_groups.crash_group_id' isn't in GROUP BY
(SQL: select * from crash_groups inner join projects on
projects.project_id = crash_groups.project_id inner join
crash_info on crash_info.crash_group_id =
crash_groups.crash_group_id where projects.organisation_id = 1
and crash_info.created_at > NOW() - INTERVAL 1 DAY group by
crash_info.crash_group_id)
If I manually take the SQL query that is within the error log and run that directly in the database, I then get 2 rows back as I am expecting so I don't understand why I'm getting an SQL error about the Group By when Eloquent executes when the query that it actually builds and logs as being an issue actually works.
Correct answer for this question is to either include the columns in a select like
$issues = \DB::table('crash_groups')
->select('crash_info.crash_group_id')
->join('projects', 'projects.project_id', '=', 'crash_groups.project_id')
->join('crash_info', 'crash_info.crash_group_id', '=', 'crash_groups.crash_group_id')
->where('projects.organisation_id', $organisation_id)
->where('crash_info.created_at', '>', $time_interval_sql)
->groupBy('crash_info.crash_group_id')
->get();
Another way is to disable mysql strict mode.
To disable strict mode, edit in in config/database.php.
'mysql' => [
'strict' => false, //'strict' => true,
],
This question already has answers here:
Group by not working - Laravel
(8 answers)
Closed 3 years ago.
I get an error in Laravel 5.4 trying to run the following query:
return ReferLinkVisit::where('refer_link_id', $this->id)
->groupBy('ipaddr')
->get()
Error:
SQLSTATE[42000]: Syntax error or access violation: 1055 'database.refer_link_visits.id' isn't in GROUP BY (SQL: select * from `refer_link_visits` where `refer_link_id` = 1 group by `ipaddr`) (View: /resources/views/dashboard/refer/home.blade.php)
Yet I can run the command in phpmyadmin and it will work just fine. I don't get it because I've wrote similar queries a hundred times but for whatever reason this time it just doesn't want to work. I can't figure out what I've done wrong.
Table structure:
You need to disable MySQL strict mode.
Open config/database.php then find mysql array within the connections array.
change the value of strict mode to false.
'connections' => [
...
'mysql' => [
...
'strict' => false, // ensure strict mode is set to false
...
],
...
]
I hope this solves your problem.
I am trying to execute some raw queries in laravel 5.3. the queries are simple but im getting errors.
table name: users
columns: id|name|email|phone|created_at|updated_at
my query:
SELECT created_at AS member_since, count(*) as row_count
FROM users
GROUP by MONTH(created_at);
This raw query works fine when I execute this on phpmyadmin. But when I execute this using laravel's database query builder i get error.
SQLSTATE[42000]: Syntax error or access violation: 1055
'query.users.created_at' isn't in GROUP BY
SQLSTATE[42000]: Syntax error or access violation: 1055
'query.users.created_at' isn't in GROUP BY (SQL: select created_at AS
member_since from `users` group by MONTH(created_at))
here is my controller:
$users = DB::table('users')
->select(DB::raw('created_at AS member_since', 'count(*) AS row_count'))
->groupBy(DB::raw('MONTH(created_at)'))
->get();
return response()->json($users);
Please correct me if Im wrong. Are there any better ways to execute raw queries?
First, you need to understand the error you're getting. This question is all about it https://stackoverflow.com/a/38551525/2474872
Now, with that in mind you have two choices
Disable only_full_group_bysetting.
Follow the indications provided
by the error message:
$users = DB::table('users')
->select(DB::raw('created_at AS member_since, count(*) AS row_count'))
->groupBy(DB::raw('MONTH(created_at)'), 'created_at')
->get();
groupBy(DB::raw('MONTH(created_at)'), 'created_at') can also be groupBy(DB::raw('MONTH(created_at), created_at')
This might be obvious, and I may be completely missing the point, but I've got a table which contains multiple rows with user_id, site_id, e.g.
ldstaff | ld_site
____________________
3 1
3 2
4 1
A MySQL query originally written by an old dev got the staff id find their name and then concat all the sites together to make something like this.
StaffID | Name | sites
3 Dave 1,2
This query was written initially as
SELECT `ld_staff` as staffID ,a.name,GROUP_CONCAT(`ld_site`) as sites FROM `lead_distribution` l
LEFT JOIN users a ON a.crm_id = l.ld_staff
WHERE ld_enabled = 1 AND
`account_type` = 1 AND `active` = 1 AND `no_more` = 0 AND network_team > 0 AND renewal_team = 0
GROUP BY `ld_staff`
However whenever we try to write it in Laravel as part of a new system to get the same results we have the error SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #2 no matter how we try to tamper with the query it always comes back to this. Reading up it seems sql_mode only_full_group_by doesn't allow this type of query, so I've changed sql_mode to = " " and this error persists.
Query
$data = DB::table('lead_distribution')
->join('users', 'ld_staff', '=', 'users.crm_id')
->select("lead_distribution.ld_staff", 'users.name',
DB::raw("GROUP_CONCAT(`ld_site`) as sites"))
->where('users.account_type', '=', 1)
->where('users.active', '=', 1)
->where('users.no_more', '=', 0)
->where('users.network_team', '>', 0)
->where('users.renewal_team', '=', 0)
->groupBy('lead_distribution.ld_staff')
->get();
#jaysingkar
Answer fixed it, my stupid mistake for missing it.
in config/database.php
Change 'strict' => true To 'strict' => false
config/database.php
Change 'strict' => true To 'strict' => false
and clear the cache
php artisan config:cache
then issue will be resolved.
I encountered this problem and got it solved by including the aggregate function in the GROUP BY clause.
I forgot where I have read this, but Aggregate functions must be included in the specified sql_mode only_full_group_by mode.
So, try using
->groupBy(['lead_distribution.ld_staff','sites']) or ->groupBy(['lead_distribution.ld_staff','users.name','sites'])
A handy tip
Try dumping the SQL Query produced by laravel by replacing ->get() with ->toSql() then try running the query produced by laravel and see if the error occurs.
We are deploying the RT index in our architecture. But we need some clarification and some difficulties faced during the deployment.
Schema defined in Index:
index logtable
{
type = rt
path = /usr/local/sphinx20/var/data/logtable
rt_attr_string = TransactionId
rt_attr_uint = CustomerId
rt_attr_timestamp = DateOfTransaction
rt_attr_string = CustomerFeedback
rt_field = TransactionType
}
Faced Problem
Question 1:
How we can get the count() query result in SPHINXQL. Because its important for us, based on customer count we have to take it to show in our application.
Example below,
Query - select count(*) from logtable where CustomerId='871';
In SphinxQL - We didnt get this result and getting the following error.ERROR 1064 (42000): index logtable: invalid schema: Count(*) or #count is queried, but not available in the schema.
Question 2:
i declared as a STRING attribute in conf for the field of "TransactionId", but i cant able to retrieve the records if that fields use in where condition.
Example below,
select * from logtable where TransactionId='TRA23454';
Following error i am getting,
ERROR 1064 (42000): sphinxql: syntax error, unexpected $undefined, expecting CONST_INT or CONST_FLOAT or '-' near '"TRA23454"'
Please help us to close these issues if knows.
Kumaran
select * from logtable where TransactionId='TRA23454';
Answer :
select * from logtable where MATCH('#TransactionId TRA23454')
In first example instead of count(*) you need to use 'show meta;' query after search query, it will contain total_count field.
select id from logtable where CustomerId='871';
show meta;
In the second example string attributes can't be used in WHERE, ORDER or GROUP clauses.
Actually you need to convert TransactionId into integer and use integer attribute. It is quite simple to do using crc32 mysql function.