Select the values of a row only once when a certain value of a column is repeated

3

I have this table and I need to select certain rows only once, according to the three different values of the column "hours" that can be seen:

Keep in mind that no will know the values of the hours column to make the query, then you can not do something like SELECT * FROM mitabla WHERE horas = "09:00 - 12:00" LIMIT 1 .

The result I'm looking for is this:

The query I am looking for should detect when there is a value equal to the previous one in the hours column and do not deliver the data, but only when a different value is found in that column.

I could simply do a SELECT * FROM mitabla and then do what I want in PHP, but I guess if I get a query that brings me only the necessary data, I would be doing a more efficient job.

If it's not possible what I'm looking for, thank you very much for your time.

    
asked by Roberto Sepúlveda Bravo 18.10.2016 в 23:36
source

2 answers

0

You can use the DISTINCT on the attribute hours and this will cause you to return only the rows that have that different column.

SELECT DISTINCT horas, lunes, martes, miercoles, jueves, viernes, sabado, domingo 
FROM mitabla GROUP BY horas

Do not forget to add the GROUP BY hours, so you get the results you are looking for.

    
answered by 18.10.2016 / 23:38
source
2

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.

  • answered by 19.10.2016 в 04:59