group by产生错误#1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregate

在MySQL5.7及以上版本中,由于新的SQL模式要求,GROUPBY分组必须包含在SELECT的所有非聚合列中。为避免only_full_group_by错误,可以使用DISTINCT替换GROUPBY来去除重复数据。示例中展示了如何修改查询语句,用distinct关键字实现分页查询时去除重复数据的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

应用场景:用于分页查询时去除重复数据 ,原代码如下:

        select mem.*, substring_index(gmo.ou,'.',2) ou,
        gmom.power_type powerType,gmom.member_type memberType
        from gm_member as mem
        left join gm_org_member as gmom on mem.member_id=gmom.member_id
        left join gm_org as gmo on gmom.grid_id=gmo.grid_id
        where mem.valid = 'Y'
        and gmo.ou_uuid like concat('%',#{pOrgId},'%')
        GROUP BY mem.member_id
        order by gmom.power_type,mem.sort asc

错误日志:#1055 - Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column ‘id’ which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by

错误原因: Mysql5.7版本以上对group by 分组有了新需求,要求group by 后的字段要与select后查询的字段一致,否则就会报错

解决方法 :使用distinct函数取代group by,修改后的代码如下:

        select distinct mem.*, substring_index(gmo.ou,'.',2) ou,
        gmom.power_type powerType,
        gmom.member_type memberType
        from gm_member as mem
        left join gm_org_member as gmom on mem.member_id=gmom.member_id
        left join gm_org as gmo on gmom.grid_id=gmo.grid_id
        where mem.valid = 'Y'
        and gmo.ou_uuid like concat('%',#{pOrgId},'%')
        order by gmom.power_type,mem.sort asc

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

隆里卡那唔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值