请教个问题,想做个简单关键字排名的功能,sql上遇到了问题。
两个表 一个shop(店铺)  一个ranking(排名表)
shop是店铺表 ranking是排名表shop表---包含店铺的基本信息 主键ID 自增  tag 标签用于搜索关键字。ranking表----包含店铺id(shopid),店铺名称(shopname),关键字(keyword),排名(sort---排名第几)shop表的ID 和 ranking表的shopid是对应的,shop和ranking可能是一对多的关系,也就是说一个店铺可以开通多个排名服务
搜索的时候先搜索有排名服务的店铺,然后根据tag字段搜索普通店铺,普通店铺搜索用like '%%'就可以我写了一部分sqlselect * from shop where id in(select shopid from ranking where keyword='关键字' order by sort)
untion all
select * from shop where not in (select shopid from ranking where keyword='关键字')后面那个select 还得加上 where tag like '%关键字%',我加上就出错还有个需求就是 分页 也就是根据untion all 所得的结果集 查询 pager(页数)*pagesize(每页信息数)之间的数据基础比较薄弱,研究好几天实在写不出来了,各位大哥帮忙研究研究吧。

解决方案 »

  1.   

    select a.* from shop a join ranking b on a.id=b.shopid
    where (b.keyword='关键字' or a.tag like '%关键字%')
    order by case when b.keyword='关键字' then 0 else 1 end,sort
      

  2.   


    select * from shop where id in(select shopid from ranking where keyword='关键字' order by sort)
    untion all
    select * from shop where id not in (select shopid from ranking where keyword='关键字')
    and tag like '%关键字%'
      

  3.   


    兄弟 a.tag like '%关键字%' 这个条件为什么不起作用
      

  4.   


    这个有错啊 ,除非另外还指定了 TOP 或 FOR XML,否则,ORDER BY 子句在视图、内联函数、派生表、子查询和公用表表达式中无效。
      

  5.   


    select * from shop where id in(select shopid from ranking where keyword='关键字')
    untion all
    select * from shop where id not in (select shopid from ranking where keyword='关键字')
    and tag like '%关键字%'--这样试试
      

  6.   


    with t
    as(
    select * from shop where id in(select shopid from ranking where keyword='关键字')
    union all
    select * from shop where id not in (select shopid from ranking where keyword='关键字')
    and tag like '%关键字%'
    )
    select top 10 * 
    from t a where type='P'
    and not exists(
    select 1 from t b where a.type=b.type
    and b.id=20 and a.id<=b.id
    )
    order by id
      

  7.   


    with t
    as(
    select * from shop where id in(select shopid from ranking where keyword='关键字')
    union all
    select * from shop where id not in (select shopid from ranking where keyword='关键字')
    and tag like '%关键字%'
    )
    select top 10 * 
    from t a where not exists(
    select 1 from t b where b.id=20 and a.id<=b.id
    )
    order by id更正一下
      

  8.   


    兄弟 在帮帮我吧,我自己写了个存储过程,就用的你的语句 ,又遇到问题了
    alter procedure [dbo].[shopsearch]
    @keyword varchar (200),
    @pagesize int,
    @pageNo int,
    @TotalRecord int output --返回总条数 
    as
    declare @strSQL varchar(5000) -- 主语句 
    begin

    with t
    as(
    select * from restaurant where id in(select top 100 percent shopid from shopsort where keyword=@keyword order by sort)
    union all
    select * from restaurant where id not in (select shopid from shopsort where keyword=@keyword)
    and tag like '%'+@keyword+'%'
    )
    set @strSQL='select top ' + str(@PageSize)+ ' * from t where id not in (select top ' + str((@PageNo-1)*@PageSize) + ' id from t)'  >>set附近语法错误exec(@strSQL)
    select @TotalRecord=count(id) from t
    end太麻烦你了 谢谢 有空帮我看看吧
      

  9.   


    存储过程里面不可以使用with  as公用表达式,你把它换成临时表,这个会吧。
    然后拼接语句你把它print出来找错误