使用only_full_group_by

时间:2018-03-08 14:06:59

标签: mysql

我试图让下面的语句与mysql v5.7.5一起使用,虽然真的根本不理解错误消息中的措辞,文档也没有帮助我。

有问题的SQL语句:

    SELECT 
        YEARWEEK(ts) AS YW, 
        DATE(ts) AS D,  
        SUM(`group-size`) AS `sum` 
    FROM `event` 
    GROUP BY YEARWEEK(ts);

我得到的错误:

Expression #2 of SELECT list is not in GROUP BY clause and contains
nonaggregated column 'event.ts' which is not functionally dependent on
columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

有人可以用英语解释我怎么能让它运作起来?对我来说,SQL语句非常有意义,但每次只是抛出错误。

1 个答案:

答案 0 :(得分:1)

请参阅longer answer

简而言之,您需要在GROUP BY中添加不包含聚合函数的所有列。在这种特殊情况下," date(ds)"需要添加:

SELECT 
    YEARWEEK(ts) AS YW, 
    DATE(ts) AS D,  
    SUM(`group-size`) AS `sum` 
FROM `event` 
GROUP BY YEARWEEK(ts), DATE(ts);