mysql implements lookups and toppings based on multiple fields

  • 2020-06-07 05:25:15
  • OfStack

mysql looks for multiple fields

In mysql, if you want to sort by a field, you can use the following SQL statement

SELECT * FROM 'TABLE_NAME' ORDER BY 'Field'

However, what if you want to implement sorting by one field and then by another? You can use the SQL statement below

SELECT * FROM 'TABLE_NAME' ORDER BY FIELD1, FIELD2;

If you want to put it in order

SELECT * FROM 'TABLE_NAME; ORDER BY FIELD1 DESC, FIELD2;

The realization of the top function

Here is an example of sorting two fields

I believe that you have all seen the top post in BBS, and you have also used the steps of the top post, so, have you ever thought about how to achieve the top function?
I need to implement such a function in a project, my practice is to add a field in the database, the field identifies the weight of posts, the high weight will be ranked forward, if the weight is equal to the sorting according to time, so as to achieve the top function.

SELECT * FROM 'TABLE_NAME' ORDER BY PIORITY DESC, DATA DESC;

Related articles: