MSSQL implementation MySQL limit SQL statement

Source: Internet
Author: User
Tags mssql mysql tutorial

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.