SQL multi-condition multi-field sorting (Graphic tutorial)

Source: Internet
Author: User

Statement:
Copy codeThe Code is as follows:
Select * from [User] order by [Password] desc, Id asc

If one condition is repeated, the second condition is used for sorting.
As follows:

Note the 14th and 15th records here.
(Sort by Id when the Password is the same)

Statement:
Copy codeThe Code is as follows:
Select top (5) * from (select top (5) * from [User] order by Id desc) x order by [Password] asc

Sort the queried (sorted) data
(That is, sort the first five records by Id and then sort the five records by Password)

Copy codeThe Code is as follows:
Select top (5) * from [User] order by Id desc

As follows:



Copy codeThe Code is as follows:
Select top (5) * from (select top (5) * from [User] order by Id desc) x order by [Password] asc

As follows:

Make an address book today because the customer's requirements are complicated .......

I have found some materials online for sorting by more conditions.
Copy codeThe Code is as follows:
Select * from Reply where Replyid = "& request (" Replyid ") &" order by sorting Field 1 desc, sorting Field 2 desc, sorting Field 3 desc

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.