我试图在 ms sql server 中对结果进行分页,但在获得正确的结果时遇到了一些麻烦。我想达到与 mysql:s LIMIT 相同的结果,我正在尝试使用这个模型来做到这一点:
SELECT * FROM (
SELECT TOP x * FROM (
SELECT TOP y fields
FROM table
WHERE conditions
ORDER BY table.field ASC) as foo
ORDER by field DESC) as bar
ORDER by field ASC
来自:http: //josephlindsay.com/archives/2005/05/27/paging-results-in-ms-sql-server/
列出前 30 行的原始查询如下:
SELECT TOP 30 pt.[BSNR], t.ID, pt.RESDATUMTID, pt.LAND1, pt.HPL1, pt.ANKDATUMTID, pt.LAND2, pt.HPL2
FROM [statistik2].[dbo].[ttrip] AS t
JOIN [statistik2].[dbo].[tparttrip] AS pt
ON t.ID = pt.TRIP_ID
WHERE t.DBKRDAT > '2012-06-27'
ORDER BY pt.BSNR DESC, t.ID, pt.RESDATUMTID
我的尝试是:
SELECT * FROM (
SELECT TOP 10 * FROM (
SELECT TOP 30 pt.ID AS PTID, pt.[BSNR], t.ID, pt.RESDATUMTID, pt.LAND1, pt.HPL1, pt.ANKDATUMTID, pt.LAND2, pt.HPL2
FROM [statistik2].[dbo].[ttrip] AS t
JOIN [statistik2].[dbo].[tparttrip] AS pt
ON t.ID = pt.TRIP_ID
WHERE t.DBKRDAT > '2012-06-27'
ORDER BY pt.BSNR DESC, t.ID DESC, pt.RESDATUMTID
) as pttt
ORDER BY pttt.PTID DESC) AS ptttt
ORDER BY ptttt.PTID
查询的输出: http: //speedy.sh/5NQeq/sqloutput.txt
有人可以解释我做错了什么吗?