日期:2014-05-17  浏览次数:20740 次

求優化
SQL code
select name from STUDENT where id in (select studentid from EXAM where score in
(SELECT score FROM EXAM 
WHERE lessonid='10004' group by score having score <=
(select avg(score) from EXAM where Lessonid ='10004') )and lessonid ='10004')

這個查詢語句有沒有簡單一點的寫法呢
或者用存儲過程?

------解决方案--------------------
你的语句怎么要4层查询啊?太复杂了吧?不就是找出10004这门课低于平均分的学生吗?试试下面的,没数据不要测
SQL code
SELECT  name
FROM    STUDENT
WHERE   id IN ( SELECT  studentid
                FROM    EXAM
                WHERE   lessonid = '10004'
                        AND score <= ( SELECT   AVG(score)
                                       FROM     EXAM
                                       WHERE    Lessonid = '10004'
                                     ) )