15

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.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
Ben Shelock
  • 20,154
  • 26
  • 92
  • 125
  • 1
    I think what you meant is "columns" not "tables" - the table is what holds all this data, a column is "season", "episode", etc. – Paolo Bergantino Jun 03 '09 at 11:04
  • 1
    Possible duplicate of [PHP MySQL Order by Two Columns](http://stackoverflow.com/questions/514943/php-mysql-order-by-two-columns) – Jim Fell Jun 13 '16 at 19:39

2 Answers2

26

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.

Don't Panic
  • 41,125
  • 10
  • 61
  • 80
Stefan Gehrig
  • 82,642
  • 24
  • 155
  • 189
4

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

Rashmi Pandit
  • 23,230
  • 17
  • 71
  • 111