Home >Database >Mysql Tutorial >How to Prioritize MySQL Table Rows by Rating and Date?

How to Prioritize MySQL Table Rows by Rating and Date?

Mary-Kate Olsen
Mary-Kate OlsenOriginal
2024-12-06 16:51:181036browse

How to Prioritize MySQL Table Rows by Rating and Date?

Prioritizing Rows in a MySQL Table: Sorting by Multiple Columns

You wish to organize your MySQL table by two criteria: highest ratings and most recent dates. To achieve this, you need to implement multi-column sorting.

Originally, you employed the following SQL statement:

ORDER BY article_rating, article_time DESC

However, this approach only sorted by a single column (article_rating). To sort by two columns, your query requires modifications.

Solution:

By default, sorting is performed in ascending order. To induce descending order for both columns, append the keyword "DESC" after each.

Here's the revised query:

ORDER BY article_rating DESC, article_time DESC

This modification ensures that the data is ordered first by article_rating (highest to lowest), and within that ranking, the articles are sorted by article_time (most recent to least recent).

The above is the detailed content of How to Prioritize MySQL Table Rows by Rating and Date?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn