我正在尝试创建一个查询,以从性别平均身高中获取更高的人的名字。
我知道如何获取每个性别的平均身高:SELECT avg(height), gender from client group by gender
但是我不知道如何将每个人(基于他的性别)与此子查询进行比较。
这是我所能做的:
SELECT cname
FROM (SELECT height, cname, gender
FROM client AS PplHeight
HAVING (height > (SELECT avg(height) from client group by gender))) AS AboveAvg
提前致谢。
最佳答案
要么加入:
select client.*
from client
join
(
select gender, avg(height) as avg_height
from client
group by gender
) genders on client.gender = genders.gender and client.height > genders.avg_height;
或使用相关子查询:
select *
from client
where height >
(
select avg(height)
from client all_clients
where all_clients.gender = client.gender
);
关于mysql - 比较单个字段与SQL中其他字段分组的同一字段的平均值,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54029634/