Join duplicate rows in mysql

0

I need to create a query prepared now I use

select restante,cajas,calidad, count(*) as total from separacion_detalles group by calidad

I do not know much about sql, in the calidad column I need to make inner join to the quality table so that it shows me the name not the id.

This is how my table is currently:

  • As you can see the boxes do not add up How can I do that?
  • is the remaining column practically the same as boxes as I can add all the boxes?
  • an idea of how it should look:

    ---------- ---------- ---------- ---------- 
    restante      cajas    calidad     total
    -------------------------------------------
       40          40        CAL1        3
       40          40        CAL2        3
       20          20        CAL3        1
    

    The idea is simply to combine the qualities that are repeated in a single row.

        
    asked by DoubleM 16.04.2018 в 20:54
    source

    1 answer

    1

    It would be as follows:

    select restante,cajas,
    SUM(cajas), /*se suma las cajas*/
    ca.nombre, /*Con el alias de la tabla "calidad", llama la columna que guarda el nombre */
    count(*) as total 
    from separacion_detalles sd, 
         calidad ca /*asignamos un Alias a la tabla de "calidad"*/
    where sd.calidad=ca.calidad  /*Se hace el enlace entre las tablas*/
    group by 4; /*agrupa por la tercera columna de la consulta, que en este caso sería agrupar por la "calidad"*/
    
        
    answered by 16.04.2018 / 21:01
    source