I recommend using the following sentence, which is a bit more complex, but guarantees stable results for what you are asking:
select t1.*
from tbl t1
join (select horas, min(fila) as min_fila
from tbl
group by horas) t2
on t2.horas = t1.horas
and t2.min_fila = t1.fila
order by t1.fila
For the benefit of other readers, it is worth explaining more in detail why the other proposed solution is not advisable.
The other proposed sentence is:
SELECT DISTINCT horas, lunes, martes, miercoles, jueves, viernes, sabado, domingo
FROM mitabla
GROUP BY horas
Firstly, it is worth mentioning that the DISTINCT
does not fulfill any function in this sentence, and it can be removed without affecting the results:
SELECT horas, lunes, martes, miercoles, jueves, viernes, sabado, domingo
FROM mitabla
GROUP BY horas
Although it may seem that the sentence is correct, we must take into account the following points:
This type of statement, where columns are included in the select
that are not part of the group by
and that are not used in aggregation functions (as is the case with the columns lunes
, martes
, miercoles
, etc), is not valid in the vast majority of popular databases. So it's good to get used to not writing sentences in this way at once.
Even MySQL is leaving behind this type of sentence. As of MySQL 5.7.5, the default configuration makes these statements illegal and result in the error: Expression ... of SELECT list is not in GROUP BY clause and contains nonaggregated column '...' which is not functionally dependent on columns in GROUP BY clause . So this same statement may not work at all if you get to change MySQL database. You can find more information in this regard here: ONLY_FULL_GROUP_BY mode .
Even though you can seem that the sentence gives you the correct results, in a way, you can say that this is by accident. The reality is that you have no guarantee that the results for the columns lunes
, martes
, miercoles
, etc ... will always be what you expect. In the documentation on MySQL Handling of GROUP BY , there explain how the database manages this case:
In this case, the server is free to choose any value from each group, so unless they are the same, the values chosen are indeterminate , which is probably not what you want Furthermore, the selection of values from each group can not be influenced by adding an ORDER BY
clause . Result set sorting occurs after values have been chosen, and ORDER BY
does not affect which value within each group the server chooses.
... that translated into Spanish, says more or less the following:
In this case, the server can choose any value from each group, then, unless all the values are the same, the chosen values are indeterminate , that probably is not what you want. Moreover, the selection of values for each group can not be influenced by adding a ORDER BY
to the statement . Sorting records occurs after the values have already been chosen, so the ORDER BY
does not affect which value the server chooses within each group.
In other words, for the hour 09:00-12:00
, there is no guarantee that the statement will give you the values of the row 1
. The server could decide to return the values of row 2
or 3
, there is no way to be sure. And this is the case even if you add a ORDER BY
to the sentence.