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')
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,
],
I have MySQL Query (this is work) like :
SELECT
project_bills.*,
project_deliveryorder.totalcost,
project_deliveryorder.deliveryorder_number,
project_deliveryorder.deliveryorder_subject,
SUM(project_bills_payment.payment_amount) AS TOTAL
FROM
project_bills
JOIN
project_deliveryorder ON project_bills.id_deliveryorder = project_deliveryorder.id
JOIN
project_bills_payment ON project_bills.id = project_bills_payment.id_bill
GROUP BY
project_bills.id
ORDER BY
project_bills.bill_date, project_bills.id DESC;
and I tried to translate to Laravel format like :
DB::table('project_bills')
->join('project_deliveryorder', 'project_bills.id_deliveryorder', '=', 'project_deliveryorder.id')
->join('project_bills_payment', 'project_bills.id', '=', 'project_bills_payment.id_bill')
->select('project_bills.*', 'project_deliveryorder.totalcost', 'project_deliveryorder.deliveryorder_number', 'project_deliveryorder.deliveryorder_subject', DB::raw('SUM(project_bills_payment.payment_amount) AS TOTAL'))
->groupBy('project_bills.id')
->orderBy('project_bills.bill_date', 'DESC')
->orderBy('project_bills.id', 'DESC')
->get();
and then I got some error like :
SQLSTATE[42000]: Syntax error or access violation: 1055 'db_tcm.project_bills.id_deliveryorder' isn't in GROUP BY
I have read Group by not working - Laravel in Stack Overflow too, so what best way to fix this issue?
Change database config ('strict' => true)?
Disable “ONLY_FULL_GROUP_BY” on MySQL?
Or re-build the Laravel query builder?
When you have a GROUP BY in your query, you can only select columns that are in the GROUP BY, or is an aggregate function like SUM(), MIN(), MAX() etc.
If I understand your data correctly, you can extract the SUM() in a separate query, where you JOIN it into the main query.
DB::table('project_bills as pb')
->join('project_deliveryorder as pdo', 'pb.id_deliveryorder', '=', 'pdo.id')
->join(DB::raw('(SELECT id_bill, SUM(payment_amount) AS TOTAL
FROM project_bills_payment
GROUP BY id_bill) AS pbp'), 'pb.id', '=', 'pbp.id_bill')
->select('pb.*',
'pdo.totalcost',
'pdo.deliveryorder_number',
'pdo.deliveryorder_subject',
'pbp.TOTAL')
->orderBy('pb.bill_date', 'DESC')
->orderBy('pb.id', 'DESC')
->get();
Disabling strict mode or disabling ONLY_FULL_GROUP_BY is in my opinion not a good idea, and if you can avoid it by reworking your query, that's usually much better.
I have my table data like below photo.
My expected data should be like this below photo.
My implemented laravel SQL code is like below.
$img = DB::table('table_name')
->select('table_name.user_id','table_name.post_id','table_name.name')
->groupBy('table_name.user_id')
->get();
This is how I'd do it. This way it's cleaner and working for me.
$img = DB::table('table_name')->select('user_id','post_id','name')
->groupBy('post_id')
->get();
$img = DB::table('table_name')
->select('user_id','post_id','name')
->groupBy('post_id')
->get();
If you're running into this SQLSTATE[42000]: Syntax error or access violation: 1055 error be sure to check that the groupBy parameter is a valid column on the table
Joining two tables showing error:
"SQLSTATE[42000]: Syntax error or access violation: 1103 Incorrect table name 'transaction_master ' "
But in my database transaction_master is exists.
The following query:
$trans_results= DB::table('report_master as rm')->join('transaction_master ', 'transaction_master.id','=','rm.id')->select('transaction_master.name as transactionID')->get();
try this
DB::table('report_master')
->join('transaction_master', 'report_master.id', '=', 'transaction_master.id')
->select('transaction_master.name as transactionID')
->get();
Read - Laravel Query Builder Joins
You need to reverse the transaction_master.id and the rm.id in your join
I am getting error on pagination
$writers = $writer::join('categories','categories.id','=','writers.category_id')
->where([['categories.category_name', '=', $category]])->paginate(1);
Can anyone tell how to paginate an inner join query.
This is the error message I'm getting:
QueryException in Connection.php line 770: 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 writers` inner join categories on categories.id = writers.category_id where (categories.category_name = Comic))
You need to specify GROUP_BY clause on your query
$writers = Writer::join('categories','categories.id','=','writers.category_id')
->where('categories.category_name', '=', $category)
->groupBy('writers.id')
->paginate(1);
UPDATE
If you still get an error, check your config/database.php. Be sure that in mysql settingsstrict = false
UPDATE 2
strict mode works on mysql starting from 5.7. If you have mysql under 5.7, set strict => false. You can check this link for more information:
Strict mode