Sandip Nag
Sandip Nag

Reputation: 998

Select Event id from event table where event start date and event end date are same

I have a Event table.

The attributes are: event_id,event_name,start_date,end_date.

I want to know all the id's where start_date and end_date are same.

How can I achieve this in mysql?

Upvotes: 1

Views: 376

Answers (1)

Martin Schneider
Martin Schneider

Reputation: 3268

If you want to find all events that share the same start- and enddate you have to join the table with itself.

SELECT
  e1.event_id,
  e2.event_id
FROM
  event e1 JOIN event e2 ON (e1.event_id < e2.event_id AND 
                             e1.start_date = e2.start_date AND 
                             e1.end_date = e2.end_date)

Will join the event table with itself if the start_date and end_date of two distinct events are the same. The condition e1.event_id < e2.event_id is to prevent that the same event is joined with itself AND that a pair of matching events is reported twice (for example [event1,event22] and [event22,event1])

Upvotes: 1

Related Questions