Jônatas Flausino
Jônatas Flausino

Reputation: 103

Ordering results differently in MySQL

I have a table which has 3 columns. (day, month and year)

This statement gives results in chronological order.

SELECT * FROM table WHERE = 'condition' ORDER BY year, month, day

How can I get it in the inverse order?

Upvotes: 0

Views: 49

Answers (3)

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476567

You can use the ASC or DESC keywords.

https://dev.mysql.com/doc/refman/5.0/en/order-by-optimization.html

For example:

SELECT *
FROM table WHERE = 'condition'
ORDER BY year DESC,
         month DESC,
         day DESC

Upvotes: 1

tadman
tadman

Reputation: 211560

You need to invert your sort order in your query:

SELECT * FROM table WHERE = 'condition' ORDER BY year DESC, month DESC, day DESC

Having separate columns for year, month, and day is counter-productive, though, as all of these could be represented in a singular DATE type column. This can be indexed and is much faster in practice:

SELECT * FROM table WHERE ... ORDER BY date_column DESC

Upvotes: 4

Hamlet Hakobyan
Hamlet Hakobyan

Reputation: 33381

Try this:

SELECT * FROM table
WHERE = 'condition'
ORDER BY year DESC, month DESC, day DESC

Upvotes: 1

Related Questions