Search code examples
mysqlsqldoctrine-ormdql

Check how many entries are in table by date


Im trying to create a specific query. E.g i have table with events:

id |     start        |      end
-----------------------------------------
1  | 10-08-2013 12:00 | 10-08-2013  14:00
2  | 10-08-2013 12:00 | 10-08-2013  14:00
3  | 10-08-2013 15:00 | 10-08-2013  16:00

And i want to insert a new event( start: 13:00, end: 15:30 ) and before that i want to check by query how many events are on the same time. In this case results should be 3: beacause 2 events are in start time and one is on the end time.

Thanks.


Solution

  • give this a try,

    SELECT  COUNT(DISTINCT ID) totalCOunt
    FROM    tableName
    WHERE   new_startDate BETWEEN start AND end 
            OR
            new_endDate BETWEEN start AND end
    

    where new_startDate and new_endDate are the new dates of the event.