SQL Server在multisearch中的结果集中选择第n到第n个

时间:2012-08-13 10:26:36

标签: sql sql-server-2008

如下面的SQL,它将选择与搜索条件匹配的100到110结果。但我认为SQL很丑陋(使用两次条件where [name] like '%%')并且应该有一些简短的声明来实现相同的目标。

SELECT top 10 [name], [field1], [field2]
FROM [FrontPageInformation] 
WHERE [name] LIKE '%%' AND [name] NOT IN
      (SELECT TOP 100 [name] 
       FROM [EngineeringMedicine].[dbo].[FrontPageInformation] 
       WHERE [name] LIKE '%%')

有人可以帮忙吗?

非常感谢,

1 个答案:

答案 0 :(得分:4)

您可以使用ROW_NUMBER()语法获取行号

例如:

select [name],[field1],[field2] 
from
(
  SELECT [name],[field1],[field2], ROW_NUMBER() OVER (ORDER BY something) as RN
  FROM [FrontPageInformation]
  WHERE [name] like '%something%'
) v
WHERE RN between 101 and 110