Jump to content

Confusing Loop, need help


Recommended Posts

Ok, so first off, here is how the tables are setup:


Cars_Table | ID, CatID

Categories_Table | ID, Parent_ID, Name


Now, both parent categories and sub-categories are in the same table. the C_Table.CatID shows only the sub-category the item is in. How would I make a list of the distinct names of Parent category names (those with Parent_ID = 0) in ASC order, but I want to only pull the parent categories for which there is an item associated. Essentially, the makes and models of the cars are stored in the same table, the Makes are the ones with Parent_ID = 0 and the Models also have parent ID's. The Cars_Table.CatID only shows the Model ID. I'm still fairly new to PHP and this one is really confusing me O.o

Link to comment
Share on other sites

Try this:


SELECT DISTINCT makes.ID, makes.Name
FROM Categories_Table makes
JOIN Categories_Table models ON (models.Parent_ID = makes.ID)
WHERE makes.Parent_ID = 0


That will give you all makes for which there exists at least one model, assuming I understand your data structure correctly.  I'm linking the ID column of a make to the Parent_ID column of the model.

Link to comment
Share on other sites


This topic is now archived and is closed to further replies.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.