Forum Moderators: open
I have two tables
Parts which just holds the names of a lot of parts
CREATE TABLE `parts` (
`PartId` int(11) NOT NULL auto_increment,
`partname` varchar(255) default NULL,
PRIMARY KEY (`PartId`)
)
and partslink which holds which part is needed by which other part
CREATE TABLE `partslink` (
`LinkID` int(11) NOT NULL auto_increment,
`PartID` int(11) default NULL,
`LinkPartID` int(11) default NULL,
PRIMARY KEY (`LinkID`)
I need a query that will give me not only the direct links to any given part but those that are linked to the direct links as well.
So if part 3 requires parts 11,18 and 22
And 18 requires 67 101
and 11 requires 59 101
and 22 requires 47 104 224
then my query should return the following
PartNo LinkedBy
11 3
18 3
22 3
67 18
101 18
59 11
47 22
104 22
224 22
As you can see I have removed any duplicate parts as well and ordered by closest links. What would be really cool is if the linkedby field could go down another step and I could get parts 2 links down from my original part.
looking forwards to your replies.
Shumit
SELECT partslink.PartID
FROM partslink
INNER JOIN parts ON partslink.PartID = parts.PartID
WHERE partslink.LinkPartID = @PartID
UNION SELECT partslink.PartID
FROM partslink
INNER JOIN parts ON partslink.PartID = parts.PartID
WHERE partslink.PartID = @PartID
ORDER BY partslink.PartID
--@PartID being the passed variable