我想做一个相关信息的查询功能
例如输入
弹开层的问题 
我会在后台自动拆字
弹开|开层|层的|的问|问题 
然后根据这些关键字or查询
我想结果能按匹配次数来排序有什么方法

解决方案 »

  1.   

    --try:
    select 关键字,count(1) from tb where charindex(关键字,colname)>0 group by 关键字 order by count(1) desc
      

  2.   

    order by 表达式 ,用order by性能会下降。
    举个例子:KW级数据取top 10可能响应时间是3秒-5秒,加上order by可能是35-39秒。楼主想想吧。
      

  3.   


    declare @table table (col varchar(12))
    insert into @table
    select '中国日本韩国' union all
    select '中国北京奥运' union all
    select '中国北京汽车' union all
    select '上海海尔'select * from @table
    /*
    col
    ------------
    中国日本韩国
    中国北京奥运
    中国北京汽车
    上海海尔
    */
    --假设关键词是中国、北京、奥运select 
    col
    from @table
    order by (case charindex('中国',col) when 0 then 0 else 1 end  +
    case charindex('北京',col) when 0 then 0 else 1 end  +
    case charindex('奥运',col) when 0 then 0 else 1 end ) desc /*
    中国北京奥运  --有3个关键词
    中国北京汽车  --有2个关键词
    中国日本韩国  --有1个关键词
    上海海尔      --没有关键词
    */
      

  4.   

    declare @str varchar(100);
    set @str = '弹开层的问题';select
        substring(@str,number,2) as s
    into #t
    from master.dbo.spt_values
    where type='p'
       and number>=1 and number<len(@str);select 
        a.col
    from tb as a
        join #t as b
    on charindex(a.s,b.col)>0
    group by a.col
    order by sum(len(b.col)-len(replace(b.col,a.s,''))/len(a.s)) desc
      

  5.   

    declare @tb table(id int,col varchar(100));
    insert @tb select 1,'这是弹开能看明白问题?'
    union all select 2,'开层就是这样'
    union all select 3,'小梁爱兰儿'
    union all select 4,'这个问题很难,是弹开吗,还是开层的问题?'
    union all select 5,'测试数据'--查询如下:declare @str varchar(100);
    set @str = '弹开层的问题';select
        substring(@str,number,2) as s
    into #t
    from master.dbo.spt_values
    where type='p'
       and number>=1 and number<len(@str);select 
        a.id,a.col
    from @tb as a
        join #t as b
    on charindex(b.s,a.col)>0
    group by a.id,a.col
    order by sum(len(a.col)-len(replace(a.col,b.s,''))/len(b.s)) desc;--删除测试
    drop table #t/*
    id          col
    ----------- --------------------------------------
    4           这个问题很难,是弹开吗,还是开层的问题?
    1           这是弹开能看明白问题?
    2           开层就是这样(3 row(s) affected)
    */