MySQL 8.0 及以上版本默认启用了 ONLY_FULL_GROUP_BY SQL 模式,这要求 SELECT 子句中的所有列要么出现在 GROUP BY 子句中,要么在聚合函数中使用
Created|Updated
|Post Views:
实际生成中遇到的sql问题,记录一下。包含场景复现sql,隐藏实际数据。
解决方法 方法一:修改 GROUP BY 子句
将 sys_user.real_name、swimplat_member_record.cost_count 和 swimplat_member_ticket.type 添加到 GROUP BY 中。但是这可能会返回更多的行,具体取决于这些列的值是否唯一。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
SELECT sys_user.real_name AS 姓名, sys_user.phonenumber AS 电话, swimplat_member_record.cost_count AS 消费次数, swimplat_member_ticket.type AS 套票类型 FROM swimplat_member_record LEFTJOIN swimplat_member_card ON swimplat_member_card.id = swimplat_member_record.member_card_id LEFTJOIN swimplat_member ON swimplat_member.id = swimplat_member_card.member_id LEFTJOIN sys_user ON sys_user.user_id = swimplat_member.user_id LEFTJOIN swimplat_member_ticket ON swimplat_member_ticket.member_card_id = swimplat_member_card.id WHERE sys_user.phonenumber IN (xxx, xxx, xxx, xxx) GROUPBY sys_user.real_name, sys_user.phonenumber, swimplat_member_record.cost_count, swimplat_member_ticket.type;
SELECTMAX(sys_user.real_name) AS 姓名, sys_user.phonenumber AS 电话, MAX(swimplat_member_record.cost_count) AS 消费次数, MAX(swimplat_member_ticket.type) AS 套票类型 FROM swimplat_member_record LEFTJOIN swimplat_member_card ON swimplat_member_card.id = swimplat_member_record.member_card_id LEFTJOIN swimplat_member ON swimplat_member.id = swimplat_member_card.member_id LEFTJOIN sys_user ON sys_user.user_id = swimplat_member.user_id LEFTJOIN swimplat_member_ticket ON swimplat_member_ticket.member_card_id = swimplat_member_card.id WHERE sys_user.phonenumber IN (xxx, xxx, xxx, xxx) GROUPBY sys_user.phonenumber;