MSSQL implementation MySQL tutorial limit SQL statement
In MySQL, you can use limit to query the records in columns m to n columns, for example:
SELECT * FROM tablename limit m, n
However, 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 Tutorial AdventureWorks:
Select Addressid, City, StateProvinceID, PostalCode
From person. [Address]
where StateProvinceID = 58
ORDER BY Addressid
If you want to query the first 6 records in the above results, the corresponding SQL statement is:
Select Top 6 Addressid, city, StateProvinceID, PostalCode
From person. [Address]
where StateProvinceID = 58
ORDER BY Addressid
If you want to query 7th through 9th Records in the results above, the corresponding SQL statement is:
Select Top 3 Addressid, City, StateProvinceID, PostalCode
From person. [Address]
where StateProvinceID = 58
and Addressid not in (
Select Top 6 addressid from person. [Address] where StateProvinceID = Addressid
)
ORDER BY Addressid