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!