Reputation: 1160
This is most likely a simple question, but I have no idea how to solve it.
In this scenario, I have three tables. Events, ads and visits. Ads and visits both have event_id fields, and visits has an ad_id field. In the end, we want to compare two event dates.
Based on the id of an ad, i need to get all visits, where their event's date is later than the ad's event date.
I would love to provide some code, but in this case I'm pretty lost, I have no idea how to do joins upon joins like that.
Thanks.
Upvotes: 1
Views: 59
Reputation: 424993
select v.*
from ads a
join events ae on ae.id = a.event_id
join visits v on v.id = a.visit_id
join events ve on ve.id = v.event_id and ve.event_date > ae.event_date
where a.id = ?
The trick here is putting a condition in the on
clause of the join to the visit's event comparing with the ad's event.
Note too that the same table can be joined more than once by assigning different table aliases.
Upvotes: 1