MySQL Forums
Forum List  »  Stored Procedures

relationship query question
Posted by: Manhao Chen
Date: September 15, 2012 01:20AM

hi all,

i have a table called tbl_tasks which stores all the tasks entries

i have another table which looks like this

SET FOREIGN_KEY_CHECKS=0;

-- ----------------------------
-- Table structure for `tbl_relationship_lookup`
-- ----------------------------
DROP TABLE IF EXISTS `tbl_relationship_lookup`;
CREATE TABLE `tbl_relationship_lookup` (
`PK_relationship_lookup` int(11) NOT NULL AUTO_INCREMENT,
`FK_Parent_ID` int(11) NOT NULL,
`FK_Child_ID` int(11) NOT NULL,
`Relationship_Comment` varchar(255) DEFAULT NULL,
PRIMARY KEY (`PK_relationship_lookup`),
KEY `KEY_parent_id1` (`FK_Parent_ID`),
KEY `KEY_child_id1` (`FK_Child_ID`),
CONSTRAINT `KEY_child_id1` FOREIGN KEY (`FK_Child_ID`) REFERENCES `tbl_task` (`PK_task`) ON DELETE CASCADE ON UPDATE NO ACTION,
CONSTRAINT `KEY_parent_id1` FOREIGN KEY (`FK_Parent_ID`) REFERENCES `tbl_task` (`PK_task`) ON DELETE CASCADE ON UPDATE NO ACTION
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8;


what the case is about is that a task could have number of parent tasks and child tasks

which is all handled by the table above

i'm now trying to write a query to get all tasks that is not a child or a parent related to a certain task

for example

if task pk1 is a parent of task pk2

task pk2 has 1 parent which is task pk1 and also a child of task pk3

it would look like

PK_relationship_lookup | FK_Parent_ID | FK_Child_ID
1 | 1 | 2
2 | 2 | 3

i'm trying to write a query to list all the task that is not associated with a certain pk

for example, if i query for pk1, i would expect to see all my tasks EXCEPT pk 1 (itself) and 2 (already related)

so another example is if i search for pk2
i expect to see all cases EXCEPT for pk1 (already its parent) pk2 (itself) and pk3 (already a child)

so another words, anything from pk4 onwards

any ideas?

Options: ReplyQuote


Subject
Views
Written By
Posted
relationship query question
1599
September 15, 2012 01:20AM
762
September 16, 2012 10:59AM
791
September 16, 2012 12:08PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.