June 18, 2009

Sorting Multiple Columns With MySQL

Ever wanted to sort your MySQL results by several columns?

SELECT * FROM orderspecs ORDER BY height, depth, width;

Now what if you want the columns to have different sort orders in the sub-sorted columns? No problem. Just specify the order for each column.

SELECT * FROM orderspecs ORDER BY height DESC, depth ASC, width DESC;