Use mysql sorting to top down articles

Source: Internet
Author: User

What do you do when the article is top? It is nothing more than the smallest or largest sorting ID for asc or desc. Today I want to introduce the use of mysql sorting methods to quickly achieve sorting.

Today, when I was doing two development jobs, I had a requirement,
The product category page must contain a parameter, that is, the product ID,
If this ID is included in the category link, you need to rank this item in the 1st category item list,
The original idea was to modify the item in PHP and change the item to the first item after selecting the category. It was quite troublesome to think about it,
Finally, I think there is no other way. After going out, I thought it would be more convenient if mysql helped us.
So how can I use mysql sorting? ORDER BY + field + sorting method, which cannot be achieved no matter how it is sorted,
If you want to use it like where, take care of it and try it.

 

After talking about this, I am afraid that some people will not be able to understand it. I will give a diagram of the requirement directly:

Before use:
SELECT 'goods _ id' FROM 'sdb _ goods 'LIMIT 0, 30

After use
SELECT 'goods _ id' FROM 'sdb _ goods 'order by 'goods _ id' = 4 desc limit 0, 30

The product 'goods _ id' = 4 is truly exclusive, which saves trouble, haha

Let's try again,

SELECT 'goods _ id' FROM 'sdb _ goods 'order by 'goods _ id' = 4 DESC, 'goods _ id' asc limit 0, 30

I really didn't think that mysql could be like this, mighty!

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.