Mysql
 sql >> Database >  >> RDS >> Mysql

MySQL sovrappone periodi di tempo in base a criteri

Se ho capito correttamente il tuo problema, hai bisogno di un join sinistro invece di un join interno -

SELECT a.id, GROUP_CONCAT(b.id) AS abcd
FROM new_table a
LEFT JOIN new_table b ON a.id <> b.id
                      AND a.Block_ID = b.Block_ID
                      AND a.Block_Type = b.Block_Type
                      AND a.Valid = b.Valid
                      AND ((a.Valid_To BETWEEN b.Valid_From AND b.Valid_To)
                            OR (a.Valid_From BETWEEN b.Valid_From AND b.Valid_To)
                            OR (a.Valid_To = b.Valid_From)
                            OR (a.Valid_From = b.Valid_To)
                          )
GROUP BY a.id;