sql语句 分组查询前10条数据

2024-12-02 21:45:20
推荐回答(5个)
回答1:

class classid classname
product classid proname num
select top 10 c.classid ,c.classname,sum(p.num) from class c,product p where p.classid=c.classid group by c.classid order by sum(p.num) desc
有的不能用top。,不同数据库软件查询好像有细微的差别。

回答2:

select sum(a.num) as cnum, min(b.classname) as clei,min(b.classid) as cid
from class b, product a
where b.classid = a.classid
group by a.classid
order by cnum desc

如果要前 10,加TOP 10在select 后。

回答3:

select top 10 b.classid,b.classname,sum(a.num) as num

from product a,分类表 b
where a.classid=b.classid
order by num desc

回答4:

select top 10 * from (select t2.classname,sum(t1.num) as num .t1.id from product t1, class t2 where t1.id=t2.classid) t1 group by t1.num desc

回答5:

select b.classname,sum(a.num) as num

from product a left join class b
where a.classid=b.classid
order by num desc