简体   繁体   English

Laravel 5.4 Raw无法正常工作,但在mySQL中工作

[英]Laravel 5.4 Raw is not working as expected but working in mySQL

 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). 第一个查询在MySQL(Sqlyog)上运行良好。 But if i run (second query ) it on my laravel 5.4 app it says: 但是如果我在laravel 5.4应用程序上运行(第二个查询),它会说:

   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? 加入后,如何从一个表返回所有数据,并从其他表返回cont? 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. 如果我在选择中添加a.id,a.name(例如选择a.id,a.name,COUNT(DISTINCT b.id))并在GROUP BY中添加a.id和a.name,则它可以工作。 The question if i return all column from a table with count should i add all column in group by? 我是否应该从带有计数的表中返回所有列的问题,我应该在group by中添加所有列吗? I know it is odd! 我知道这很奇怪! Then how can i get my expected result in laravel 5.4 query builder? 然后,如何在laravel 5.4查询生成器中获得预期的结果?

Laravel 5.3 and 5.4 use strict mode for mysql per default. Laravel 5.3和5.4默认情况下对MySQL使用严格模式。 That means that ONLY_FULL_GROUP_BY SQL mode is also enabled. 这意味着还启用了ONLY_FULL_GROUP_BY SQL模式。 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,则可以按表的主键进行分组,并在SELECT子句中使用该表中的所有列,因为它们在功能上取决于PK。

MySQL 5.7.5 and up implements detection of functional dependence. MySQL 5.7.5及更高版本实现了对功能依赖性的检测。 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. 如果启用了ONLY_FULL_GROUP_BY SQL模式(默认情况下),则MySQL拒绝查询,其中选择列表,HAVING条件或ORDER BY列表引用未在GROUP BY子句中命名且在功能上不依赖于它们的非聚合列。 (Before 5.7.5, MySQL does not detect functional dependency and ONLY_FULL_GROUP_BY is not enabled by default. (在5.7.5之前,MySQL不会检测到功能依赖性,并且默认情况下未启用ONLY_FULL_GROUP_BY。

( MySQL Handling of GROUP BY ) MySQL处理GROUP BY

Your options are: 您的选择是:

Upgrade MySQL to at least 5.7.5 将MySQL升级到至少5.7.5

Or disable strict mode in laravels db config ( config/database.php ) 或者在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). 对MariaDB(和xampp)用户来说是个坏消息:MariaDB似乎还不支持“检测功能依赖”(尚未)。 All i could find is this ticket . 我只能找到这张

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM