Mysql supports searching and topping Multiple Fields

Source: Internet
Author: User

Mysql searches for multiple fields

In mysql, If You Want To sort a field, you can use the following SQL statement

SELECT * FROM 'table _ name' order by 'field'

However, what should I do if I want to sort a field and then another field? You can use the following SQL statement

SELECT * FROM 'table _ name' order by FIELD1, FIELD2;

If you want to add sorting

SELECT * FROM 'table _ NAME; order by FIELD1 DESC, FIELD2;

Implementation of the pin function

The following is an example of sorting two fields.

I believe you have seen top posts in BBS and you have used the top posts step. Have you ever thought about how to implement the top posts function?
To implement this function in a project, I add a field in the database to identify the weight of the post, and the row with a high weight will appear, if the weights are equal, they are sorted by time. In this way, the pin function is implemented.

SELECT * FROM 'table _ name' order by piority desc, data desc;

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.