A
A
Anton2014-09-27 21:44:10
PHP
Anton, 2014-09-27 21:44:10

How to sort a list taken from db by month and day?

There are users in the database with the date of birth:
1990-05-25
1987-02-04
1994-07-13
I make the list output like this:

SELECT * FROM users WHERE bd!='0' ORDER BY DATE_FORMAT(`bd`, '%Y-%m-%d') DESC

But the problem is that the list is sorted by date of birth incorrectly. Some users, for example, with 11 months jump over those with 10 months.
Please help me fix this.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Andrey Ezhgurov, 2014-09-27
@eandr_67

1. If you want to sort exactly by month and day without a year, then what the hell are you adding %Y to the date formatting template?
2. If you sort by full date and the bd field has type DATE, DATETIME. TIMESTAMP etc. (i.e. a type designed to store a date) then no perversions with DATE_FORMAT are required - just ORDER BY bd DESC .
3. For the situation when the field is empty, in SQL if the special value is NULL . Crutches in the form of bd!='0' - another nonsense.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question