How can I write the below raw query in Laravel
$result= DB::select('SELECT a.awb_number,a.full_awb_number,a.service_type, b.city AS From_city, c.country_code AS From_country, b.city AS To_city, c.country_code AS To_country, datediff( max(s.created_at), min(s.created_at)) as Aging FROM customer_shippments a LEFT JOIN addresses b ON a.from_address = b.id LEFT JOIN addresses c ON a.to_address = c.id LEFT JOIN statuses s ON s.awb_number = a.awb_number WHERE a.full_awb_number IN ("LA172827289EE", "LA172828854EE", "LA172829894EE") GROUP BY s.awb_number');
return $result;
While executing the above query I am getting the following error
SQLSTATE[42000]: Syntax error or access violation: 1055 'track.a.awb_number' isn't in GROUP BY (SQL: SELECT a.awb_number,a.full_awb_number,a.service_type, b.city AS From_city, c.country_code AS From_country, b.city AS To_city, c.country_code AS To_country, datediff( max(s.created_at), min(s.created_at)) as Aging FROM customer_shippments a LEFT JOIN addresses b ON a.from_address = b.id LEFT JOIN addresses c ON a.to_address = c.id LEFT JOIN statuses s ON s.awb_number = a.awb_number WHERE a.full_awb_number IN ("LA172827289EE", "LA172828854EE", "LA172829894EE") GROUP BY s.awb_number)
2
Answers
This error may occur, if you have parameter
'strict' => true
in config/database.php for you DB connection.
You can set this parameter to false, or change your query so that all the fields from ‘select’ are represented in ‘group by’
use DB::raw(), but try to sanitize your data first before use DB::raw to prevent SQL Injection
Laravel Query Docs