Remix919 Posted May 12, 2011 Share Posted May 12, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/236221-confusing-loop-need-help/ Share on other sites More sharing options...
btherl Posted May 13, 2011 Share Posted May 13, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/236221-confusing-loop-need-help/#findComment-1214776 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.