使用与sql_mode = only_full_group_by不兼容的MySQL查询

时间:2019-11-04 11:50:37

标签: mysql

我的Query

出错
  

1055 SELECT列表的表达式#2不在GROUP BY子句中,并且   包含未聚合的列'dms.HrAttLogsFormatted.ScanIn',该列为   在功能上不依赖于GROUP BY子句中的列;这是   与sql_mode = only_full_group_by

不兼容

查询:

SELECT
    Employee.Id as Id,
    Employee.FingerId as FingerId,
    Employee.Name as Name,
    Departement.Departement as Departement,
    EmployeeShift.Shift as Shift,
    ScanIn
FROM
    HrEmployee as Employee
    LEFT JOIN HrEmployeeShift as EmployeeShift 
        ON Employee.ShiftId = EmployeeShift.Id
    LEFT JOIN CmDept as Departement 
        ON Employee.DeptId = Departement.Id
    LEFT JOIN (
        SELECT
            TableEmployee.FingerId,
            ScanIn
        FROM
            HrEmployee as TableEmployee,
            HrAttLogsFormatted
        WHERE
            TableEmployee.FingerId = HrAttLogsFormatted.FingerId
            AND DateIn = '2019-11-04'
        GROUP BY HrAttLogsFormatted.FingerId
    ) AS HrJoinLogs 
        ON Employee.FingerId = HrJoinLogs.FingerId
WHERE
    Employee.Status = 1
    AND Employee.Flag = 1
    AND Employee.ShiftId = 1
    AND ScanIn is NULL
GROUP BY
    Employee.Name
ORDER BY
    Employee.Name ASC

有人可以解决吗?

1 个答案:

答案 0 :(得分:0)

首先,了解MySQL的notorious non-standard handling of GROUP BY. Here.

第二,您似乎正在使用GROUP BY从结果集中消除重复项。该程序值得怀疑。我认为当您的查询涉及其他人的钱时,这是不可接受的。您可能要改用SELECT DISTINCT。但是,最好的选择是找出此查询的预期逻辑,然后重写它以更明确地传递该逻辑。

如果您仍然认为需要GROUP BY,请在升级到更新版本的MySQL之前先查看查询开始的结果集。您将看到结果集的ScanIn列的所有行均为NULL,因为查询显示为AND ScanIn IS NULL

因此,按ScanIn分组不会造成任何伤害。将, ScanIn添加到两个GROUP BY子句中。