A related example:
table activity
activity id | title
1 a
2 a2
3 b
4 b2
table people
id | parent id | activity id | active
Grab all the people who:
- is in a specific activity without a parent
- is in a specific activity who’s parent is not in the same activity + not in the same related activity (ie: a, a2)
So far I have something less than ideal:
SELECT p.pid FROM people p
left join activity a
on p.activityid = a.activityid
where p.active = 1
and a.title = 'a'
and EXISTS (
SELECT p1.pid from people p1
left join activity a1
on p1.activityid = a1.activityid
where p.parentid = p1.id
and a1.activityid != p.activityid
or p.parentid = 0
)
This doesn’t account for parentids not ‘a2’ or ‘b2’ etc..
The thing is I have the target activity id I want, but not the related activity, so I figured I’d somehow use the title instead?
You don’t need a outer join. The
not exixtsclause will filer out someone without parents.