14

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
  • 39,820
  • 10
  • 58
  • 75
Ben Shelock
  • 18,888
  • 26
  • 91
  • 124
  • 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

25

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
  • 39,820
  • 10
  • 58
  • 75
Stefan Gehrig
  • 80,936
  • 24
  • 154
  • 184
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

SO User
  • 22,482
  • 16
  • 67
  • 111