从学生和标记表中选择具有最大分数的学生

时间:2015-04-21 06:34:55

标签: mysql sql database

我有两张桌子学生和标记。学生表有标记和标记表格标记和标记。我想找一个成绩最高的学生。怎么做?

2 个答案:

答案 0 :(得分:2)

我从你的问题得到的,你需要有最高分的学生

试试这个:

Select st.stid ,  st.name, m.marks
from student st
inner join mark m on st.stid = m.stid
order by m.marks desc limit 1

答案 1 :(得分:0)

我假设您想要学生分数的最大平均值(因为您写的是“最高分数的学生” - 所以我假设一名学生有很多分数。

这里是示例模式,数据和查询:

create table student (
    stid int,
    name varchar(100)
)

create table mark (
    stid int,
    mark int
);

insert into student values(1, 'a');
insert into student values(2, 'b');
insert into student values(3, 'c');

insert into mark values(1, 5);
insert into mark values(1, 4);
insert into mark values(2, 6);
insert into mark values(2, 5);
insert into mark values(3, 4);
insert into mark values(3, 4);

select s.stid, s.name, a.avg_mark
from (select stid, AVG(mark) as avg_mark from mark group by stid) as a, student s
Where a.avg_mark =
    (select Max(avg_mark) as max_mark from
        (select  stid, AVG(mark) as avg_mark from mark group by stid) as x)
and
s.stid = a.stid;