2

打印参加最多课程的学生的姓名和 sid(s)

注册

sid  class number

1     23
2     54
1     54
3     43
1     43
2     43

学生 sid 的名字


1    sagar
2    kiran
3    ravi
4    vishal

输出

sid   sname

1     sagar
4

2 回答 2

6

按学生分组注册,按人数排序和使用限制1:

select s.id, s.name
from student s
join enroll e on e.sid = s.id
group by s.id, s.name
order by count(*) desc
limit 1

请注意您不需要选择 count(*)- 您可以简单地参考它。

于 2013-07-16T11:53:58.867 回答
1

我想这会对你有所帮助

SELECT <column_name> FROM <table_name> WHERE <column_name>=
(SELECT <column_name>
 FROM (SELECT <column_name>, count(*) as cnt FROM <table_name> GROUP BY <column_name>) AS foo
 WHERE foo.cnt=(SELECT MAX(c) FROM (SELECT <column_name>,count(*) AS c FROM <column_name> GROUP BY <column_name>) AS bar)) limit 1
于 2015-12-15T11:51:31.043 回答