0

我有一个大型查询,可以根据搜索拉回联系人。这是查询的简化示例。(表和字段名称已更改以保护无辜。)

SELECT DISTINCT TOP 20 DB1.FieldID, DB1.Field1, DB.Field2, DB.Field3,
DB2.Field88, DB3.Field77
FROM tblLamp DB1
LEFT JOIN tblSand DB2 ON DB2.FieldID = DB1.FieldID
LEFT JOIN tblLime DB3 ON DB3.FieldID = DB1.FieldID
WHERE (Field1 LIKE 'hotwet%' ESCAPE '!' OR Field2 LIKE 'maarten%' ESCAPE '!')
AND DB2.Field99 != '1111-2222-3333-4444'
AND (DB1.CreatorID IN (...) OR DB1.OwnerID IN (...) OR DB1.FieldID IN (...))
ORDER BY DB1.Field1 ASC, DB1.Field2 ASC

我在网上找到了很多使用 ROW_NUMBER() 的示例,但我无法让它与这个查询一起工作。有任何想法吗?

4

1 回答 1

0

您可以使用这个小示例作为指导:

declare @test table(id int, name varchar(255));
insert into @test
  select 1, 'one' union
  select 2, 'two' union
  select 3, 'three' union
  select 4, 'four' union
  select 5, 'five' union
  select 6, 'six';

select
  *
from
(
  -- select the columns you need and include row number to enable limit 
  select
    name,
    row_number() over(order by id) as rownumber
  from
    @test
) v1
where
  rownumber between 2 and 4
于 2012-10-04T22:33:00.827 回答