1. MySQL :
SELECT a.*,COUNT(DISTINCT b.id) AS IndianTruck,COUNT(DISTINCT d.id) AS BdTruck
FROM manifests a
JOIN truck_entry_regs b ON a.id = b.manf_id
LEFT JOIN truck_deliverys d ON a.id = d.manf_id
WHERE a.manifest='550/7'
GROUP BY a.id
2.Laravel 5.4
$results = DB::select('SELECT a.*, COUNT(DISTINCT b.id) AS IndianTruck,COUNT(DISTINCT d.id) AS BdTruck
FROM manifests a
JOIN truck_entry_regs b ON a.id = b.manf_id
LEFT JOIN truck_deliverys d ON a.id = d.manf_id
WHERE a.manifest=?
GROUP BY a.id', ['550/7'])
I have the same query. The first query run well on MySQL(Sqlyog). But if i run (second query ) it on my laravel 5.4 app it says:
QueryException in Connection.php line 647:
SQLSTATE[42000]: Syntax error or access violation: 1055 'dbblpa.a.port_id' isn't in GROUP BY (SQL: SELECT a.*, COUNT(DISTINCT b.id) AS IndianTruck,COUNT(DISTINCT d.id) AS BdTruck
FROM manifests a
JOIN truck_entry_regs b ON a.id = b.manf_id
LEFT JOIN truck_deliverys d ON a.id = d.manf_id
WHERE a.manifest=550/7
GROUP BY a.id)
How can i return all data from a table with cont from other tables after joining? If i add a.id,a.name in the select (like select a.id,a.name, COUNT(DISTINCT b.id)) and add a.id and a.name in GROUP BY , then it works. The question if i return all column from a table with count should i add all column in group by? I know it is odd! Then how can i get my expected result in laravel 5.4 query builder?
Laravel 5.3 and 5.4 use strict mode for mysql per default. That means that ONLY_FULL_GROUP_BY
SQL mode is also enabled. But if your MySQL version is at least 5.7.5 you can group by a primary key of a table and use all columns from that table in the SELECT clause because they are functionally dependent on the PK.
MySQL 5.7.5 and up implements detection of functional dependence. If the ONLY_FULL_GROUP_BY SQL mode is enabled (which it is by default), MySQL rejects queries for which the select list, HAVING condition, or ORDER BY list refer to nonaggregated columns that are neither named in the GROUP BY clause nor are functionally dependent on them. (Before 5.7.5, MySQL does not detect functional dependency and ONLY_FULL_GROUP_BY is not enabled by default.
( MySQL Handling of GROUP BY )
Your options are:
Upgrade MySQL to at least 5.7.5
Or disable strict mode in laravels db config ( config/database.php
)
// ..
'connections' => [
// ..
'mysql' => [
// ..
'strict' => false,
// ..
],
// ..
]
Update
Bad news for MariaDB (and xampp) user: MariaDB seems not to support the "detection of functional dependence" (yet). All i could find is this ticket .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.