情况是这样,一个类似商城的网站,有很多店铺,店铺里有很多商品,用户可以将他们一些最新最有特色的商品推荐出来。
现在想这么查询:查询出10条最新的推荐商品,但是每个店铺只查询出2个。

解决方案 »

  1.   


    select top10 * from 
    (
    select top 2 from 店铺1 where 特色='asd' order by id desc
    union all
    select top 2 from 店铺2 where 特色='asd' order by id desc
    union all
    select top 2 from 店铺3 where 特色='asd' order by id desc
    union all
    select top 2 from 店铺4 where 特色='asd' order by id desc
    union all
    select top 2 from 店铺5 where 特色='asd' order by id desc
    )t
      

  2.   

    SELECT * FROM TABLE1 a WHERE EXISTS (  SELECT TOP 2 * FROM TABLE1 WHERE A=a.xx and B=a.xx )
      

  3.   

    SELECT top 10 * FROM TABLE1 a WHERE EXISTS (  SELECT TOP 2 * FROM TABLE1 WHERE A=a.xx and B=a.xx )
      

  4.   

    union 不能同时使用 order by
    使用临时表吧
    select top 2 * into #tb from 商品 where 店铺="aa" order by 特色
    insert into #tb select top 2 * from 商品 where 店铺="bb" order by 特色
    insert into #tb select top 2 * from 商品 where 店铺="cc" order by 特色
    select * from #tb
    drop table #tb
      

  5.   

    数据库是sccess的,在程序里语句怎么写?
      

  6.   

    不好意思,没有说清楚。就一张商品表sp ,有编号id ,所属商铺编号spid ,推荐 tj ,1代表推荐,0为否。
      

  7.   

    access里一些属性不支持,遍历商铺循环实现
    select distinct(spid)from sp