我在解决SQL查询方面遇到了一些问题。
有两个表table1和table2。
table1有studentId
(主键)和name
table2有studentId
(FK),subject
,marks
我正在尝试编写一个查询,主题明确排名前三位的得分手以及他们得分的标记。
在Oracle11g中,我可以使用分区功能轻松完成。如何在MySQL中做同样的事情?
修改: 我创建了表并输入了值:
CREATE TABLE table1
(
rollno int NOT NULL UNIQUE PRIMARY KEY,
name VARCHAR(60) NOT NULL
);
CREATE TABLE table2
(
rollno int not null,
subject varchar(60) not null,
marks int not null,
foreign key (rollno) references table1(rollno)
);
insert into table1 values(32,"john");
insert into table1 values(33,"amber");
insert into table1 values(34,"meher");
insert into table1 values(35,"lucy");
insert into table1 values(36,"philips");`
insert into table2 values(32,"maths",82);
insert into table2 values(32,"english",28);
insert into table2 values(32,"science",46);
insert into table2 values(33,"maths",67);
insert into table2 values(33,"english",89);
insert into table2 values(33,"science",72);
insert into table2 values(34,"maths",83);
insert into table2 values(34,"english",65);
insert into table2 values(34,"science",78);
insert into table2 values(35,"maths",98);
insert into table2 values(35,"english",87);
insert into table2 values(35,"science",81);
insert into table2 values(36,"maths",56);
insert into table2 values(36,"english",67);
insert into table2 values(36,"science",43);`
我使用以下代码:
select *
from
( Select t1.name, t2.subject as su, t2.marks
from table1 t1
inner join table2 t2 on t1.rollno=t2.rollno
order by t2.marks
)t
order by su, marks desc;
但是这段代码打印所有学生列表主题明智。我在限制进入前三名时遇到了问题