我建议您在单个子查询中计算最小值和最大值。然后,您可以简单地使用where
子句过滤您想要的行:
SELECT s.`pres_id`, COUNT( `pres_id` ), ROUND( AVG( `tot_score` ) , 2 ) ,
`username`, `name`, `pres_day`
FROM `scores` s join
`users` u
on u.id_user = s.pres_id join
(select s.pres_id, max(tot_score) as maxts, min(tot_score) as mints
from scores s
group by s.pres_id
) ssum
on u.id_user = ssum.pres_id
where s.tot_score > ssum.mints and s.tot_score < ssum.maxts
GROUP BY s.`pres_id`;
我猜你想去掉每个学生的最高分和最低分。如果您只想省略所有学生的最高和最低,请使用以下内容:
SELECT s.`pres_id`, COUNT( `pres_id` ), ROUND( AVG( `tot_score` ) , 2 ) ,
`username`, `name`, `pres_day`
FROM `scores` s join
`users` u
on u.id_user = s.pres_id cross join
(select max(tot_score) as maxts, min(tot_score) as mints
from scores s
) ssum
where s.tot_score > ssum.mints and s.tot_score < ssum.maxts
GROUP BY s.`pres_id`;
编辑:
当学生只有一个分数时进行修改以保留分数:
SELECT s.`pres_id`, COUNT( `pres_id` ), ROUND( AVG( `tot_score` ) , 2 ) ,
`username`, `name`, `pres_day`
FROM `scores` s join
`users` u
on u.id_user = s.pres_id join
(select s.pres_id, max(tot_score) as maxts, min(tot_score) as mints,
count(*) as cnt
from scores s
group by s.pres_id
) ssum
on u.id_user = ssum.pres_id
where (s.tot_score > ssum.mints and s.tot_score < ssum.maxts) or (cnt = 1)
GROUP BY s.`pres_id`;