In MySQL, you can use limit to query records from column M to column N. For example:
CopyCode The Code is as follows: Select * From tablename limit m, n
However,The limit statement is not supported in SQL Server. What should we do?
Solution:
Although SQL server does not support limit, it supports top.
Taking SQL Server 2005 as an example, we use its own demo database adventureworks as the test data:Copy codeThe Code is as follows: Select ID from tablename
If you want to query the first six records in the preceding results, the corresponding SQL statement is:
Copy codeThe Code is as follows: Select top 6 ID from tablename
If you want to query 7th to 9th records in the preceding results, the corresponding SQL statement is:
Copy codeThe Code is as follows: Select top 3 ID from tablename
Where id not in (
Select top 6 ID from tablename
)
Copy codeThe Code is as follows: Select top (n-m + 1) ID from tablename
Where id not in (
Select top M-1 ID from tablename
)
Copy codeThe Code is as follows: Select top @ pagesize ID from tablename
Where id not in (
Select top @ offset ID from tablename
)