Here is my table's (events) content. eventID is "primary key" and parentID is "foreign key" with references to events(eventsID)
self referenced table :
eventID eventName parentID appFK 1 evt1 null 2 2 evt2 1 1 3 evt3 1 1 4 evt4 null 3 5 evt5 8 3 6 evt6 null 4 7 evt7 null 1 8 evt8 null 1
and another table content (applications) like this :
appID appName 1 app1 2 app2 3 app3 4 app4
I'd like to fetch all eventIDs which are parents or not with a given appID. If a child has the given appID, i'd like to get his parentID and not himself. So the result is going to be like this with appID = 1 :
eventID eventName ParentID appFK 1 evt1 null 2 // parent event who has children with appID = 1 7 evt7 null 1 // event with no child and appID = 1 8 evt8 null 1 // parent event with appID = 1 and has a child
I tried lot of examples and read a lot of solutions here but i didn't find a problem like this. Can you help me to write the right SQL ?
thx.