Grouping and ordering The manual says "MySQL extends the use of GROUP BY so that you can use
non-aggregated columns or calculations in the SELECT list that do not
appear in the GROUP BY clause... Do not use this feature if the columns
you omit from the GROUP BY part are not constant in the group. The
server is free to return any value from the group, so the results are
indeterminate unless all values are the same."
I'm looking for a way around that in MySQL (as opposed to having to do
it in PHP or something.) Is there one? An example is following:
I have a database of TV listings. Each show is assigned an identifier,
unique to that episode. I would like a list showing airings of show X,
but with duplicates removed. This I have done successfully, GROUPing
by the episode identifier. The only problem is that a random airing of
the episode is displayed; I'd like to have the first one displayed.
A very simplified example, this episode of '24' is on at 1am and 11am
on channel 28, 9pm on channel 46:
| episode | name | start | channel |
+---------+------+-------+---------+
| s04ep02 | 24 | 01:00 | 28 |
| s04ep02 | 24 | 11:00 | 28 |
| s04ep02 | 24 | 21:00 | 46 |
If I "SELECT start, channel FROM programs WHERE name='24' GROUP BY
episode" I will get a random selection from the list. Trying to use,
for example, MIN(start) does give me the first start time, but
everything else is still from a random row. |