我是用 peewee 这个 ORM 给我翻译 sql 并查询数据

翻译出来的 sql 如下,可以看到其实比较不规范,group by 里面只有 track_source_id,但是 select 里面却有 parser_name、error_class_name、track_source_id

select
    `t1`.`parser_name`,
    `t1`.`error_class_name`,
    `t1`.`track_source_id`,
    COUNT('*') as `task_count`
from
    `parse_monitoring_detail` as `t1`
where
    (`t1`.`success` = 0)
group by
    `t1`.`track_source_id`
order by
    `task_count` desc

正常情况下,执行上面的 sql 应该报错

SQL 错误 [1055] [42000]: Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'crawler2.t1.parser_name' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

但是实际上却没有,为什么?

因为 peewee 创建连接之后,会先执行 SET sql_mode='PIPES_AS_CONCAT'

具体可看: peewee 创建连接前的前置操作,wireshark 抓包


universe_king
3.4k 声望677 粉丝