假设有个产品表products 一个大类表bigcls 小类表smallCls.
products表 有proname BigId smallId
bigcls表有  bigId
smallCls表有  smallId
我想查询出每个类别的中有多少产品,SQL怎么写?类似淘宝的搜索结果筛选。
 当别人搜索一个产品时,自动显示有匹配到此关键字的 类别(并且数量)显示出来。比如我搜索关键字:"白色"
显示是: 
美容(5534)  
服饰(4887)
鞋包(522)
数码(216)
因为搜索一个关键字可能有很多不同的类别。
请问这样的sql语句怎么写?
 

解决方案 »

  1.   


    select t2.name,count(t1.*) 
    from products t1
    left join bigcls t2 on t1.BigId=t2.BigId
    where t2.name like '%白色%'
    group by name
      

  2.   


    --1.
    select bigid,smallid,count(*) from products group by bigid,smallid
      

  3.   

    select m.proname , 
           (select count(1) from bigcls n where n.bigId = m.bigId),
           (select count(1) from smallCls t where t.smallId = m.smallId) 
    from products m
    where m.proname like '%白色%'