日期:2014-05-19  浏览次数:20477 次

求SQL语句问题
Table   admin
adminID       adminName     admintype
1                     xiaoming         3
2                     xiaohua           2
3                     xiaowang         2
4                     yu                     1
5     lai           4
Table   userType
TypeID TypeName
1 Student
2 Teacher
3 Cleaner
4 Manager
5 Counselor
按userType列出TypeID,TypeName和UserCount(用户数量),如果这个Type下没有用户,则显示0,按UserCount降序排序,然后在按TypeName升序排序。
请问怎么求啊


------解决方案--------------------
select TypeID,
TypeName,
UserCount = ISNULL((SELECT COUNT(1)
FROM [admin]
WHERE [admin].admintype = [userType].TypeID),0)
FROM [userType]
ORDER BY
ISNULL((SELECT COUNT(1)
FROM [admin]
WHERE [admin].admintype = [userType].TypeID),0) DESC