In MySQL, you can use Limit to query the records in columns m to n columns, for example:
Copy Code code as follows:
SELECT * FROM tablename limit m, n
But
Limit statements are not supported in SQL Server. What do we do?
Solution:
Although SQL Server does not support Limit, it supports top.
Let's take SQL Server 2005, for example, to test data with its own demo database AdventureWorks:
Copy Code code as follows:
if you want to query the first 6 records in the above results, the corresponding SQL statement is:
Copy Code code as follows:
Select Top 6 IDs from TableName
If you want to query 7th through 9th Records in the results above, the corresponding SQL statement is:
Copy Code code as follows:
Select top 3 IDs from TableName
where ID not in (
Select Top 6 IDs from TableName
)
Copy Code code as follows:
Select Top (n-m+1) ID from tablename
where ID not in (
Select top M-1 IDs from TableName
)
Copy Code code as follows:
Select top @pageSize ID from tablename
where ID not in (
Select top @offset ID from tablename
)