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

再来个语句,请高手帮忙简化,呵呵
SELECT   name,
                    (SELECT   COUNT(id)
                  FROM   userinfo
                  WHERE   politics   =   a.id   AND   id   IN
                                      (SELECT   id
                                    FROM   student
                                    WHERE   class   IN
                                                        (SELECT   id
                                                      FROM   class
                                                      WHERE   f_grade_id   =   1)))   AS   todal
FROM   dbo.basicmess   A
WHERE   (type   =   7)

------解决方案--------------------
try


SELECT A.name,
COUNT(E.id) AS todal
FROM dbo.basicmess A
Left Join
(Select B.id, B.politics From userinfo B
Inner Join student C On B.id = C.id
Inner Join class D On C.class = D.id
Where D.f_grade_id = 1) E
On A.id = E.politics
WHERE (A.type = 7)
Group By A.name