【问题标题】:select a student with max marks from student and marks table从学生和分数表中选择一个分数最高的学生
【发布时间】:2015-04-21 15:01:02
【问题描述】:

我有两张桌子学生和马克。学生表有 stid 和名称,其中标记表有 stid 和标记。我想找一个分数最高的学生。怎么做?

【问题讨论】:

  • 是否是一对一的,即分数表中每个学生一条记录?
  • 成绩最高的学生是作业不问别人的人...

标签: mysql sql database


【解决方案1】:

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

试试这个:

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

【讨论】:

  • 嗨,Sashi,我们可以编写上面的查询而不像使用子查询那样进行内部连接吗?
【解决方案2】:

我假设您想要学生平均分数的最大值(正如您写的“具有最高 分数 的学生” - 所以我假设一个学生有很多分数。

这里是示例架构、数据和查询:

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;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-30
    • 2021-05-08
    • 2021-07-18
    • 1970-01-01
    • 2022-11-18
    相关资源
    最近更新 更多