P
I recommend using the following sentence, which is a little 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 sentence proposed is:SELECT DISTINCT horas, lunes, martes, miercoles, jueves, viernes, sabado, domingo
FROM mitabla
GROUP BY horas
First, it's worth mentioning that DISTINCT does not perform any function in this sentence, and can be removed without affecting the results:SELECT horas, lunes, martes, miercoles, jueves, viernes, sabado, domingo
FROM mitabla
GROUP BY horas
Although it might seem that the sentence is correct, the following points should be taken into account:This type of sentence, where columns are included in the select which are not part of the group by and that are not used in aggregation functions (as is the case with columns) lunes, martes, miercoles, etc), is not valid in most popular databases. So it's good to get used to it once and not writing sentences this way.Even MySQL is leaving this kind of sentence behind. From MySQL 5.7.5, the default setting makes these sentences illegal and result in 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 sentence may not work at all if you change the MySQL database. You can find more information here: http://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_only_full_group_by .Even if I can It seems 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, miercolesThey'll always be what you expect. In the documentation http://dev.mysql.com/doc/refman/5.7/en/group-by-handling.html , there explains how the database handles 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 cannot 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 of each group, then, unless all values are the same, the chosen values are undeterminedWhich is probably not what you want. It's more, the selection of values for each group cannot be influenced by adding a ORDER BY to the judgement. Sorting records occurs after the values have already been chosen, so that 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 sentence will give you the values of the row 1. The server could decide to return the values in the row 2 or 3There's no way to be sure. And this is the case even if you add a ORDER BY to the sentence.