日期:2014-05-18  浏览次数:20347 次

查询第二名成绩。。这样写不对好像。求指教,谢谢
SQL code
select top(1) stu_score from
                (select top(2) stu_score from tb_score where course_id=
                (select course_id from tb_course where course_name = 'java') order by stu_score Desc )
                order by stu_score ASC


------解决方案--------------------
SQL code
select top(1) stu_score from
                (select top(2) stu_score from tb_score where course_id=
                (select course_id from tb_course where course_name = 'java') order by stu_score Desc --这样选出来的是倒数第二句的成绩)
                order by stu_score ASC

--#1.
select top(1) stu_score from
(select top(2) stu_score from tb_score where course_id=
(select course_id from tb_course where course_name = 'java') order by stu_score asc) T
order by stu_score desc
    
--#2.
select * from
(
    select
        rowno = row_number() over(order by a.stu_score),
        *
    from tb_score a
        inner join tb_course b
            on a.course_id = b.course_id
    where b.course_name = 'java'
) T
where rowno = 2