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。,不同数据库软件查询好像有细微的差别。
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 后。
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
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
select b.classname,sum(a.num) as num
from product a left join class b
where a.classid=b.classid
order by num desc