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

有大神能帮忙看下这句sql么。。。
现在做一个图书网站,根据用户买过得最多的书的类别,给他推荐该类的书籍。现在我写了一段查询:
SQL code


(select type,count(type) type_count from
(select type from book where id in
(select bookid from OrderDetails where orderid in
(select id from orders where userid = 
(select id from userinfo where name='ccccc')))) a group by type ) b)


查询出的结果为:
type type_count
20 1
23 6
25 1

我现在想取出其中type_count为最大的那一行的type值,遇到了问题。。试了半天也不行, 求大神解决下。

------解决方案--------------------
SQL code

(select TOP 1 type,count(type) type_count from
(select type from book where id in
(select bookid from OrderDetails where orderid in
(select id from orders where userid = 
(select id from userinfo where name='ccccc')))) a group by type ) b)
ORDER BY COUNT(type) DESC