Hello Everyone,
I am currently stuck on how the concept of a self-join works. At least in the format I want it.
I need to create a query that will show only Active parts. Parts are being replaced by new revisions and new models constantly.
I have one table:
-
Field Type Null Default Links to Comments MIME
-
ModelID int(10) No
-
ModelName varchar(25) No
-
Discontinued enum('TRUE', 'FALSE') No 'FALSE'
-
ReplacedWithModelID int(10) Yes NULL tblmodel -> ModelID
-
ModelImage varchar(200) Yes NULL
-
As you can see ReplacedWithModelID looks up ModelID.
If I have a table with this data:
-
ModelID ModelName ReplacedWithModelID
-
1 ModelA
-
2 ModelB 3
-
3 ModelC 4
-
4 ModelD
-
How would I create a query that has results like.
-
ModelID ModelName
-
1 ModelA
-
4 ModelD
-
If I have only one replacement I can create the query, but as the list of replacements gets longer 2 by 3, 3 by 4... I end up getting information that I do not want.
-
SELECT DISTINCTROW tblModel.ModelName, tblModel.ModelID, tblModel.ReplacedWithModelID
-
FROM tblModel
-
WHERE (((tblModel.ReplacedWithModelID) Is Null)) OR (((tblModel.ModelID) In (SELECT tblModel.ReplacedWithModelID
-
FROM tblModel
-
WHERE (((tblModel.ReplacedWithModelID) Is Not Null)))));
-
Please help me to figure this out.
Thank you!