I have a one to many relation that looks like this:
| Parent | | Child |
| id | | id |
| | |parentID|
| | | date |
And I am trying to structure a query such that I get all of the parents who have children records which ALL have a date before a specified date.
Something like this
SELECT * FROM parent
JOIN child on child.parentid = parent.id
WHERE child.date <= '10/13/2010'
But the problem with this is I get parents that have children with a date before the date specified and have child records with a date after the date specified, when I want ONLY the parents of children with a date before the given date.
Does anyone have some suggestions on how to handle this case?
Thanks!
Use:
Everyone will tell you to use JOINs “because they’re faster”, but typically they aren’t aware of the impact of using them — if you don’t need the information from a supporting table, you shouldn’t be joining to it. That’s because more than one child in this situation would produce duplicate PARENT records. The trade-off between a JOIN and DISTINCT or GROUP BY vs IN or EXISTS is probably par, but without the hassle of dealing with the duplicated data properly.