我正在使用 MYSQL 来计算学生 table 每个科目的 TOP-N。
I am using MYSQL for calculating the TOP-N in each subject in a student table.
我有一个学生 table,他有 student_id、分数和科目
CREATE TABLE IF NOT EXISTS students
(student_id INT(3), subject ,VARCHAR(45), score INT(3) );
插入的数据为
insert into students values(1,'math',70);
insert into students values(1,'science',71);
insert into students values(1,'history',72);
insert into students values(1,'english',73);
insert into students values(1,'kannada',74);
insert into students values(3,'math',50);
insert into students values(3,'science',51);
insert into students values(3,'history',52);
insert into students values(3,'english',53);
insert into students values(3,'kannada',54);
insert into students values(2,'math',60);
insert into students values(2,'science',61);
insert into students values(2,'history',62);
insert into students values(2,'english',63);
insert into students values(2,'kannada',64);
我在使用查询后得到了所需的输出,
select student_id,score,subject
from
(select @prev := '', @n:=0) init
join
(select @n := if(subject != @prev , 1, @n+1) as n,
@prev := subject,
student_id,score,subject from students
order by
subject asc,
score desc
) x
where n<=2
order by subject, score desc;
我根本不明白这是如何工作的,为什么需要加入?这是一个子查询吗?请问from子句中的语句运行对每一行数据?有人请给我解释一下。我正在学习SQL。
注意:我在网上发现此查询与此类似,我只是对其进行了修改以满足我的要求,这不是我的工作。
刚好需要连接,以便您可以在查询中初始化变量 @prev
和 @n
。这需要与您尝试过滤的查询分开进行。您可以在查询之前执行此操作,但这会将所有内容放在一个独立的查询中。
SET @prev = '';
SET @n = 0;
SELECT student_id, score, subject
FROM
(select @n := if(subject != @prev , 1, @n+1) as n,
@prev := subject,
student_id,score,subject from students
order by
subject asc,
score desc
) x
where n<=2
order by subject, score desc;
在这种情况下使用子查询,以便您可以 select 您想要的 n <= 2
行。
我有一个学生 table,他有 student_id、分数和科目
CREATE TABLE IF NOT EXISTS students
(student_id INT(3), subject ,VARCHAR(45), score INT(3) );
插入的数据为
insert into students values(1,'math',70);
insert into students values(1,'science',71);
insert into students values(1,'history',72);
insert into students values(1,'english',73);
insert into students values(1,'kannada',74);
insert into students values(3,'math',50);
insert into students values(3,'science',51);
insert into students values(3,'history',52);
insert into students values(3,'english',53);
insert into students values(3,'kannada',54);
insert into students values(2,'math',60);
insert into students values(2,'science',61);
insert into students values(2,'history',62);
insert into students values(2,'english',63);
insert into students values(2,'kannada',64);
我在使用查询后得到了所需的输出,
select student_id,score,subject
from
(select @prev := '', @n:=0) init
join
(select @n := if(subject != @prev , 1, @n+1) as n,
@prev := subject,
student_id,score,subject from students
order by
subject asc,
score desc
) x
where n<=2
order by subject, score desc;
我根本不明白这是如何工作的,为什么需要加入?这是一个子查询吗?请问from子句中的语句运行对每一行数据?有人请给我解释一下。我正在学习SQL。
注意:我在网上发现此查询与此类似,我只是对其进行了修改以满足我的要求,这不是我的工作。
刚好需要连接,以便您可以在查询中初始化变量 @prev
和 @n
。这需要与您尝试过滤的查询分开进行。您可以在查询之前执行此操作,但这会将所有内容放在一个独立的查询中。
SET @prev = '';
SET @n = 0;
SELECT student_id, score, subject
FROM
(select @n := if(subject != @prev , 1, @n+1) as n,
@prev := subject,
student_id,score,subject from students
order by
subject asc,
score desc
) x
where n<=2
order by subject, score desc;
在这种情况下使用子查询,以便您可以 select 您想要的 n <= 2
行。