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

MySQL overlappende tidsperioder baseret på kriterier

Hvis jeg forstår dit problem korrekt, har du brug for en venstre join i stedet for indre join -

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;



  1. bruger sql union på samme tabel i cakephp find forespørgsel

  2. Hvordan forbinder man MySQL-database til C# WinForm Application?

  3. Hvordan resultaterne bliver blandet mellem to tabeller, når du bruger UNION

  4. SQL-forespørgsel til trætabel