Mario
Mario

Reputation: 885

mysql order by str_to_date doesn't work

i have a table with date(actually they are string time in this format: 2004-Mar). I want to rank the record based on this date, so i have the following query:

SELECT *,STR_TO_DATE(detail,'%Y-%b') 
FROM table2 JOIN user_table 
ON table2.user_id = user_table.id 
ORDER BY STR_TO_DATE(detail,'%Y-%b') DESC 
WHERE table2_col = 11;

But this query doesn't work, it asks me to check syntax err near 'WHERE table2_col = 11' at line 5

If i delete ORDER BY STR_TO_DATE(detail,'%Y-%b') DESC , then everything works fine. so i think the error comes from str_to_date? what's wrong with my code? thanks

Upvotes: 1

Views: 1962

Answers (1)

Sashi Kant
Sashi Kant

Reputation: 13465

The order by should come after the where clause

Try this::

SELECT *,STR_TO_DATE(detail,'%Y-%b') 
FROM table2 JOIN user_table 
ON table2.user_id = user_table.id 

WHERE table2_col = 11
ORDER BY STR_TO_DATE(detail,'%Y-%b') DESC 
;

Upvotes: 2

Related Questions