日期:2014-05-18 浏览次数:20474 次
select a.userid, score=(select sum(score) from tab where userid=a.userid) from (select 1 as userid union select 2 union select 3 union select 4 union select 5 union select 6) a order by 2 desc
------解决方案--------------------
SELECT ISNULL(b.score, 0) score , a.number userid FROM master..spt_values a LEFT JOIN ( SELECT SUM(score) score , userid FROM tab WHERE userid IN ( 1, 2, 3, 4, 5, 6 ) GROUP BY userid ) b ON a.number = b.userid WHERE a.type = 'P' AND a.number IN ( 1, 2, 3, 4, 5, 6 )
------解决方案--------------------
select isnull(n.score,0) score , m.userid from ( select 1 userid union select 2 userid union select 3 userid union select 4 userid union select 5 userid union select 6 userid ) m left join ( select sum(score) score,userid from tab where userid in(1,2,3,4,5,6) group by userid ) n on m.userid = n.userid order by score desc , m.userid