MYsql5.7版本之后,用group by查询不在分组字段遇到的坑

select uid,name from (select uid,name from aa union select uid,name from bb) t group by uid order by uid desc LIMIT 10;

image.png

运行上面那个SQL会报错,因为5.7版本之后的MYSQL不在group by里的字段 跟在select会报错解决办法是,用函数 any_value(字段名)

select any_value(name),max(uid)from (select uid,name from aa union select uid,name from bb) t group by uid order by uid desc LIMIT 10;
运行上面的SQL 不报错了

image.png

any_value(字段名)允许,非分组字段的出现

你可能感兴趣的:(MYsql5.7版本之后,用group by查询不在分组字段遇到的坑)