我正在使用我的应用上的分数排名来获取所有用户分数。我的问题是我不知道如何为每个stud_num返回一行。
我的查询:
SELECT * FROM score WHERE assess_type = 'professional' ORDER BY total_score DESC.
正如你所看到的,我有3个stud_num,我只想要每个stud_num一行,并且得分最高。
答案 0 :(得分:1)
您可以使用这样的相关查询:
SELECT * FROM score t
WHERE t.assess_type = 'professional'
AND t.total_score = (select max(s.total_score)
from score s
where t.stud_num = s.stud_num)
group by stud_num
答案 1 :(得分:0)
使用MAX和GROUP BY函数,如下所示:
SELECT score_id, stud_num, assess_type, total_item, MAX(total_score), average, date_taken
FROM score
WHERE assess_type = 'professional'
GROUP BY stud_num
ORDER BY 5 DESC
答案 2 :(得分:0)
@sagi给出的选项很好:
SELECT * FROM score t
WHERE t.assess_type = 'professional'
AND t.total_score = (select max(s.total_score)
from score s
where t.stud_num = s.stud_num)
group by stud_num
另一个选择是一起使用内连接和组。
生成的查询将变为:
select * from score a
inner join (
SELECT stud_num, max(total_score) tscore FROM `score` group by stud_num) b
on a.stud_num = b.stud_num and total_score= tscore
group by a.stud_num
尝试
答案 3 :(得分:-1)
这是我的答案: SELECT score_id,stud_num,assess_type,total_item,MAX(total_score) 从得分 WHERE assessment_type ='professional' GROUP BY stud_num,total_item ORDER BY MAX(total_score)DESC