我有一个包含3列的表格student_id
,section_id
和marks
,每个列都包含整数值,student_id
是具有唯一值的主要字段。现在我需要为下面的3个场景编写查询(PS:这仅用于学习目的,如果可能的话,如果可以向我展示一些替代的逻辑方法,那将会很棒):
对于我试过的第一个:
SELECT * FROM `students` s
WHERE s.student_id=
(
SELECT student_id
FROM students
WHERE s.section_id=section_id
ORDER BY marks DESC
LIMIT 2,1
)
GROUP BY section_id
ORDER BY marks DESC
虽然我不确定优化,但这有效。
对于下一个我尝试使用IN
因为否则我无法在子查询中获取多行,但它说mysql不支持LIMIT
IN
有没有办法为第二种情况修改这个查询?
最后,我不知道如何处理第三个问题。
我在http://sqlfiddle.com/#!2/6442fb/1
创建了一个测试表任何帮助表示感谢。
答案 0 :(得分:0)
不幸的是,MySQL仍然缺乏对分析功能的支持。具体而言,RANK()
和RANK_DENSE()
会非常有用。模仿它们的一种方法是使用用户变量:
SELECT student_id, section_id, marks
FROM
(
SELECT s.*, @n := IF(@s = section_id, IF(@m = marks, @n, @n + 1), 1) rank,
@s := section_id, @m := marks
FROM students s CROSS JOIN (SELECT @n := 0, @s := NULL, @m := NULL) i
ORDER BY section_id, marks DESC
) q
WHERE rank = 3
ORDER BY marks DESC;
SELECT student_id, section_id, marks
FROM
(
SELECT s.*, @n := IF(@s = section_id, IF(@m = marks, @n, @n + 1), 1) rank,
@s := section_id, @m := marks
FROM students s CROSS JOIN (SELECT @n := 0, @s := NULL, @m := NULL) i
ORDER BY section_id, marks DESC
) q
WHERE rank BETWEEN 2 AND 4
ORDER BY section_id, marks DESC;
SELECT student_id, section_id, marks
FROM
(
SELECT s.*, @n := IF(@s = section_id, @n + 1, 1),
@rn := IF(@m = marks, @rn, @n) rank,
@s := section_id, @m := marks
FROM students s CROSS JOIN (SELECT @n := 0, @rn := 0, @s := NULL, @m := NULL) i
ORDER BY section_id, marks DESC
) q
WHERE rank = 4
ORDER BY marks DESC;
这是 SQLFiddle 演示