"If a worker wants to do his job well, he must first sharpen his tools." - Confucius, "The Analects of Confucius. Lu Linggong"
Front page > Programming > How to Sort a MySQL Table by Multiple Columns (Rating then Date)?

How to Sort a MySQL Table by Multiple Columns (Rating then Date)?

Published on 2025-01-29
Browse:953

How to Sort a MySQL Table by Multiple Columns (Rating then Date)?

Sorting MySQL Tables by Multiple Columns

To order a MySQL table by multiple columns, specify the columns in the ORDER BY clause separated by commas. By default, sorting is ascending. To sort in descending order for a specific column, use the DESC keyword after the column name.

In your case, you want to sort articles by highest ratings first and then by most recent date. Use the following query:

ORDER BY article_rating DESC, article_time DESC

This will produce the following output:

 ================ ============================= ============== 
| article_rating | article                     | article_time |
 ================ ============================= ============== 
| 50             | This article rocks          | Feb 4, 2009  |
 ---------------- ----------------------------- -------------- 
| 35             | This article is pretty good | Feb 1, 2009  |
 ---------------- ----------------------------- -------------- 
| 5              | This Article isn't so hot   | Jan 25, 2009 |
 ================ ============================= ============== 
Latest tutorial More>

Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.

Copyright© 2022 湘ICP备2022001581号-3