Getting rows filtered by most recent time mysql

前端 未结 2 1022
误落风尘
误落风尘 2021-01-29 06:02

I have a question of a complex query and I don\'t know how to write it. I have the next table:

+---------------+-----------+----------+--------------------------         


        
相关标签:
2条回答
  • 2021-01-29 06:46

    To retrieve the correct value of idaction, you could use group_concat in combination with substring_index. Note that you don't really need to join the table alarmas, as you have all you need in nectar_incidencias_alarma:

    SELECT     id_alarma,
               SUBSTRING_INDEX(GROUP_CONCAT(idaction 
                               ORDER BY CONCAT(fecha, ' ', hora) DESC), ',', 1)+0 id_action,
               MAX(CONCAT(fecha, ' ', hora)) 
    FROM       nectar_incidencias_alarma
    WHERE      idaction IN (6,7) 
    GROUP BY   id_alarma
    
    0 讨论(0)
  • 2021-01-29 06:50

    Try this query :-

    select * from (
    select * from (
    SELECT     a.id, 
               nia.idaction grp_is, 
               MAX(CONCAT(nia.fecha, ' ', nia.hora)) time_is
    FROM       nectar_incidencias_alarma nia 
    INNER JOIN alarmas a 
            ON a.id=nia.id_alarma 
    WHERE      nia.idaction IN (6,7) 
    GROUP BY   a.id
    ) tab1
    order by tab1.time_is desc) tab2
    group by tab2.grp_is
    
    0 讨论(0)
提交回复
热议问题