0

这是我的问题。我有一个这样的选择语句:

select * from mytable where mycol like '%finance%'

结果如下所示:

id  mycol 
16  the finance department 
 8  i like the finance people 
43  chief finance officer 
22  finance 
68  finance trading

我想让订单像这样出来:

id  mycol 
22  finance 
68  finance trading 
43  chief finance officer 
16  the finance department 
 8  i like the finance people

本质上,要让选择查询使用:

where mycol = 'finance'

然后

where mycol = 'finance%'

然后

where mycol = '%finance%'

1) 我不能将 id 列用于订购目的。

2)我的查询比这复杂得多(它是使用 SELECT TOP 1000000 ROW_NUMBER() OVER (ORDER BY t.category, t.sequence) 在大型数据库上分页记录集的一部分...

我想我可能需要使用 UNION 或 RANK() 或其他东西,但我现在已经脱离了我的联盟。非常感谢任何指导:)

4

1 回答 1

1

一个明显的解决方案:

Select Id, mycol
    , Case
        When mycol = 'finance' Then 1 
        When mycol Like 'finance%' Then 2
        When mycol Like '%finance%' Then 3
        End As Rnk
From MyTable
Where mycol Like '%finance%' 
Order By Rnk

当然,这与前导通配符和尾随通配符一起表现不佳。另一种方法是使用全文搜索,这将为您提供相关性排名。

CONTAINSTABLE (Transact-SQL)

于 2011-03-11T06:19:34.793 回答