I have a table which holds information on television programs and I want to order it by Seasons and then by episodes. Here's a basic view of what I have:
+---+--------+---------+
|id | Season | Episode |
+---+--------+---------+
| 1 | 1 | 1 |
+---+--------+---------+
| 1 | 1 | 2 |
+---+--------+---------+
| 1 | 2 | 1 |
+---+--------+---------+
| 1 | 2 | 3 |
+---+--------+---------+
So I select what I need and order by Season. But there's going to be a lot between seasons so I need to sort episodes too, but without it affecting seasons.
This sorts your MySQL table result in Ascending or Descending order according to the specified column. The default sorting order is Ascending which you can change using ASC or DESC . SELECT * FROM [table-name] ORDER BY [column-name1 ] [ASC|DESC] , [column-name2] [ASC|DESC],..
Summary. Use the ORDER BY clause to sort the result set by one or more columns. Use the ASC option to sort the result set in ascending order and the DESC option to sort the result set in descending order. The ORDER BY clause is evaluated after the FROM and SELECT clauses.
After the ORDER BY keyword, add the name of the column by which you'd like to sort records first (in our example, salary). Then, after a comma, add the second column (in our example, last_name ). You can modify the sorting order (ascending or descending) separately for each column.
Syntax: SELECT * FROM table_name ORDER BY column_name; For Multiple column order, add the name of the column by which you'd like to sort records first.
Do you mean:
SELECT id, Season, Episode
FROM table
ORDER BY Season ASC, Epsisode ASC
Sorting by multiple columns is as simple as it gets.
We know what you mean :) In your order by you should have
ORDER BY Season, Episode
It will sort by Season and then on Episode within Season
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With