mysql错误与sql_mode = only_full_group_by不兼容

时间:2019-06-30 14:18:57

标签: python mysql

我有我在云端(pivotal.io)中运行的flask应用程序,并且已订阅mysql数据库实例。我有一个可以在我的本地计算机以及任何其他mysql数据库上运行的功能性mysql查询。当我在云中运行查询时,出现以下错误消息:

  

OperationalError:(1055,“ SELECT列表的表达式#1不在GROUP中   BY子句,包含非聚合列   'service_instance_db.heatmap_history.date_added'不是   功能上取决于GROUP BY子句中的列;这是   与sql_mode = only_full_group_by不兼容”)

由于数据库在云中运行,因此我无权运行以下命令来禁用only_full_group_by模式

    SET GLOBAL sql_mode=(SELECT REPLACE(@@sql_mode,'ONLY_FULL_GROUP_BY',''));

这是我的查询,它对过去几个月的行总数进行计数,如果没有数据,则在一个月内返回0:

select
            t1.date_added,
            coalesce(SUM(t1.attempt_count+t2.attempt_count), 0) AS attempt_count
            from
            (
            select DATE_FORMAT(a.Date,'%%Y/%%m/%%d') as date_added,
            '0' as  attempt_count
            from (
            select curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY as Date
            from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
            cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
            cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
            ) a
            where a.Date BETWEEN NOW() - INTERVAL 5 MONTH AND NOW()
            )t1
            left join
            (
            SELECT DATE_FORMAT(date_added,'%%Y/%%m/%%d') AS date_added,
            COUNT(*) AS attempt_count
            FROM heatmap_history
            WHERE heatmap_history.customer_id = %s and DATE_SUB(date_added, INTERVAL 1 DAY) > DATE_SUB(DATE(NOW()), INTERVAL 5 month)
            GROUP BY month(date_added) DESC
            )t2
            on t2.date_added = t1.date_added
            group by month(t1.date_added)
            order by t1.date_added desc limit 5

此查询是否可以优化为在现有数据库中运行?还是我不得不更改我的云提供商?

预先感谢

0 个答案:

没有答案