Home > Database > Mysql Tutorial > How to Order MySQL Data by DD/MM/YYYY Date Format?

How to Order MySQL Data by DD/MM/YYYY Date Format?

Susan Sarandon
Release: 2024-12-13 17:34:09
Original
484 people have browsed it

How to Order MySQL Data by DD/MM/YYYY Date Format?

How to Order by Date Formats in MySQL

When working with date formats in MySQL, you may encounter the need to order your data according to a specific format. While the YYYY-MM-DD format is commonly used, you may also have data in the DD/MM/YYYY format. Understanding the syntax for ordering by different date formats is crucial.

To order by the YYYY-MM-DD format, you can use the following syntax:

...ORDER BY date DESC...
Copy after login

However, for the DD/MM/YYYY format, the syntax you provided:

SELECT * FROM $table ORDER BY DATE_FORMAT(Date, '%Y%m%d') DESC LIMIT 14
Copy after login

will not work.

To correctly order by DD/MM/YYYY, you need to adjust the syntax. If your desired intention is to format the output date, you can use the DATE_FORMAT() function:

SELECT *, DATE_FORMAT(date, '%d/%m/%Y') AS niceDate 
FROM table 
ORDER BY date DESC 
LIMIT 0,14
Copy after login

This will display the dates in the DD/MM/YYYY format.

Alternatively, if you indeed want to sort by Day before Month before Year, you can use a more complex query that includes additional sorting criteria:

SELECT * 
FROM table 
ORDER BY DAY(date), MONTH(date), YEAR(date) DESC 
LIMIT 0,14
Copy after login

By following these guidelines, you can effectively order your MySQL data according to the desired DD/MM/YYYY date format.

The above is the detailed content of How to Order MySQL Data by DD/MM/YYYY Date Format?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template