Mysql
 sql >> Base de Dados >  >> RDS >> Mysql

MySQL mescla 2 conjuntos de resultados excluindo registros no primeiro


Teste
SELECT id, crp, starts_on
FROM tbl_crps
NATURAL JOIN (SELECT crp, MAX(id) id
              FROM tbl_crps
              WHERE starts_on < CURRENT_DATE
              GROUP BY crp) x
UNION ALL
SELECT id, crp, starts_on
FROM tbl_crps
NATURAL JOIN (SELECT crp, MAX(id) id
              FROM tbl_crps
              GROUP BY crp
              HAVING MIN(starts_on) >= CURRENT_DATE) x;

violino