How can I create a table with a column in Date dd / mm / yyyy format and sort it later with ORDER BY?
1 answer
There is no need to interfere with the ways to store the date in the database. The base itself will figure out how it is more convenient for her to do it. And if we are talking about the presentation of the date on the user side, then there are functions for working with dates .
Example:
DROP TABLE IF EXISTS datetest; CREATE TABLE datetest ( td DATE ); INSERT INTO datetest (td) VALUES (STR_TO_DATE('15/3/2016','%d/%c/%Y')), (STR_TO_DATE('15/2/2016','%d/%c/%Y')); Sample times:
SELECT DATE_FORMAT(td, '%d/%c/%Y') FROM datetest ORDER BY td; Result:
15/2/2016 15/3/2016 Sample two:
SELECT DATE_FORMAT(td, '%d/%c/%Y') FROM datetest ORDER BY td DESC; Result:
15/3/2016 15/2/2016 - Thank you ........ - max
|
DATE. Details are in the documentation . - PinkTux