题解 | 找出每个学校GPA最低的同学

找出每个学校GPA最低的同学

https://www.nowcoder.com/practice/90778f5ab7d64d35a40dc1095ff79065

select device_id,university,gpa
from user_profile
where (university,gpa) in (select university, min(gpa) from user_profile group by university)
order by university;

先找出每个学校的最低分,在找到他们的id.

如果直接把device_id放在子查询语句里面会报错:“SQL_ERROR_INFO: "Expression #1 of SELECT list is not in GROUP BY clause and contains nonaggregated column 'user_profile.device_id' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by"”

AI解释是:这个错误是由于 MySQL 的 sql_mode 包含了 ONLY_FULL_GROUP_BY 设置,它要求 SELECT 列表中的非聚合列必须出现在 GROUP BY 子句中。

全部评论

相关推荐

评论
1
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务