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'])
我有相同的查詢。 第一個查詢在MySQL(Sqlyog)上運行良好。但是,如果我在我的laravel運行(第二查詢),其5.4的應用程序,它說:Laravel 5.4 Raw未按預期工作,但在mySQL中工作
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)
如何從一個表續加入後返回來自其他表中的所有數據? 如果我在select中添加a.id,a.name(如選擇a.id,a.name,COUNT(DISTINCT b.id))並在GROUP BY中添加a.id和a.name,那麼它就可以工作。 這個問題,如果我從表中返回所有列與計數,我應該添加組中的所有列?我知道這很奇怪! 那麼我怎樣才能讓我的預期結果laravel 5.4查詢生成器?
是否顯示.id是主鍵?或者至少是獨特的?主要關鍵是 –
。但我並沒有明確地使用truck_deliverys或truck_entry_regs製作外鍵。 – Fawel
FK對此問題無關緊要。 –