我如何使此查询工作 mysql 5.5.20 两个具有排名功能的表
how do I make this query work mysql 5.5.20 two tables with rank function
我有两个 Table
学生Table
id
adminno
fullname
1
p001
john
2
p002
Jane
3
p003
Jack
4
p004
Joan
标记table
id
adminno
term
year
marks
1
p001
Term I
2021
300
2
p002
Term I
2021
400
3
p003
Term I
2021
200
4
P004
Term I
2021
700
预期结果
id
adminno
fullname
term
year
marks
Rank
4
p004
Joan
Term I
2021
700
1
2
p002
Jane
Term I
2021
400
2
1
p001
john
Term I
2021
300
3
3
P003
Jack
Term I
2021
200
4
我的代码
SET @curRank := 0;
SELECT
students.adminno,
students.fullname,
students.id,
students.adminno,
marks.term,
marks.year,
marks.total as total,
rank FROM
(SELECT
students.adminno,
students.fullname,
students.id,
marks.adminno,
marks.id,
marks.term,
marks.year,
marks.total as total,
@curRank := IF(@prevRank = total, @curRank, @incRank) AS rank,
@incRank := @incRank + 1,
@prevRank := total
FROM marks p, (
SELECT @curRank :=0, @prevRank := NULL, @incRank := 1 )r
INNER JOIN students.adminno =marks.adminno
WHERE students.term='Term I'
ORDER BY total DESC ) s;
我遇到了这个错误
ERROR 1248 (42000): Every derived table must have its own alias
如何正确连接两个 table 以获得我想要的结果。
我是 mysql
的初学者
SELECT total.*, @rank := @rank + 1 AS student_rank
FROM ( SELECT *
FROM marks
JOIN students USING (id, adminno) ) total
CROSS JOIN ( SELECT @rank := 0 ) variable
ORDER BY total.marks DESC;
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=bc74b343be4722352c1a193bf2a709ea
我有两个 Table
学生Table
id | adminno | fullname |
---|---|---|
1 | p001 | john |
2 | p002 | Jane |
3 | p003 | Jack |
4 | p004 | Joan |
标记table
id | adminno | term | year | marks |
---|---|---|---|---|
1 | p001 | Term I | 2021 | 300 |
2 | p002 | Term I | 2021 | 400 |
3 | p003 | Term I | 2021 | 200 |
4 | P004 | Term I | 2021 | 700 |
预期结果
id | adminno | fullname | term | year | marks | Rank |
---|---|---|---|---|---|---|
4 | p004 | Joan | Term I | 2021 | 700 | 1 |
2 | p002 | Jane | Term I | 2021 | 400 | 2 |
1 | p001 | john | Term I | 2021 | 300 | 3 |
3 | P003 | Jack | Term I | 2021 | 200 | 4 |
我的代码
SET @curRank := 0;
SELECT
students.adminno,
students.fullname,
students.id,
students.adminno,
marks.term,
marks.year,
marks.total as total,
rank FROM
(SELECT
students.adminno,
students.fullname,
students.id,
marks.adminno,
marks.id,
marks.term,
marks.year,
marks.total as total,
@curRank := IF(@prevRank = total, @curRank, @incRank) AS rank,
@incRank := @incRank + 1,
@prevRank := total
FROM marks p, (
SELECT @curRank :=0, @prevRank := NULL, @incRank := 1 )r
INNER JOIN students.adminno =marks.adminno
WHERE students.term='Term I'
ORDER BY total DESC ) s;
我遇到了这个错误
ERROR 1248 (42000): Every derived table must have its own alias
如何正确连接两个 table 以获得我想要的结果。 我是 mysql
的初学者SELECT total.*, @rank := @rank + 1 AS student_rank
FROM ( SELECT *
FROM marks
JOIN students USING (id, adminno) ) total
CROSS JOIN ( SELECT @rank := 0 ) variable
ORDER BY total.marks DESC;
https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=bc74b343be4722352c1a193bf2a709ea