Filter original records, given a joined table
Given such a query:
The result gives us all the records from
I want the results of
Please also consider the following factors:
The result gives us all the records from
past_bookings, even if there are some past_booked_services records whose partner_id is not the same as the value of the partnerId variable in my code.I want the results of
past_bookings to be only those records who had at least one past_booked_services joined record whose partner_id matches the value of partnerId. Please also consider the following factors:
- Pagination is also applied to this query using
.range()as this is only a sample piece of code which is part of a more complex function so filtering from the application level is out of the question and needs to be applied from the database level. - The relationship between
past_bookingsandpast_booked_servicesis a one-to-many relationship, therefore I cannot query thepast_booked_servicestable first and then join it withpast_bookingsbecause this will result in many duplicate records ofpast_bookings.