5

我有一个有书名字段的表。我希望能够像这样对记录进行排序:

  1. 古代鳄鱼
  2. 安妮阿姨的鳄鱼
  3. 鳄鱼完整指南
  4. 乡村短吻鳄
  5. 不要碰鳄鱼!
  6. 轻松的鳄鱼狩猎

依此类推,忽略“A”、“An”和“The”,当它们作为标题的第一个单词出现时。(它们也可以在标题的任何地方被忽略。)

我知道这些是 SQL Server 2008 中的停用词,因此如果有人在搜索中使用它们,它们可以被忽略。

但是有没有办法让 ORDER BY 忽略它们?(如果有区别,查询将使用 ASP.NET 中的 LinqDataSource。)

谢谢!

4

2 回答 2

2

如果您有大量记录,则使用 replace() 计算排序键将无法扩展。

最好的方法是添加一个额外的表字段,其中包含删除了 A/An/The 等前缀的标题,并确保它具有索引以加快排序。然后您可以只按这个新字段排序,但显示原始未更改的字段。

于 2011-07-26T02:42:03.660 回答
1

可能是这样的。

;with T(Title) as
(
  select 'The Ancient Alligator'          union all
  select 'Aunt Annie''s Alligator'        union all
  select 'A Complete Guide to Alligators' union all
  select 'Countrified Alligators'         union all
  select 'Don''t Touch the Alligator!'    union all
  select 'An Effortless Alligator Hunt'
)

select Title
from T
order by replace(
         replace(
         replace(T.Title, 
         'A ', ''), 
         'An ', ''), 
         'The ', '')

结果:

Title
------------------------------
The Ancient Alligator
Aunt Annie's Alligator
A Complete Guide to Alligators
Countrified Alligators
Don't Touch the Alligator!
An Effortless Alligator Hunt
于 2011-06-21T05:38:57.277 回答