I have a question of a complex query and I don\'t know how to write it. I have the next table:
+---------------+-----------+----------+--------------------------
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