Jump to content

Newbie Needs Help with Formulating a Query


Recommended Posts

My table, 'Categories' featured below, has parent categories and child categories. A child category has a parent eg carrots (child) vegetable (parent).

 

I am trying to formulate a query which returns the *name* of the parent category rather than just the parent id and I am failing miserably. Parentid 0 is root category.

 

Many Many Thanks for you help !!

 

id 	name 	parentid
18 	Poultry 	0
19 	Beef 	0
20 	Chicken 	18
21 	Ribs 	19
22 	Fish 	0
23 	Salmon 	22
24 	Turkey 	18
26 	Community 	0
27 	Recent News 	26
28 	Honey 	0
29 	Organic Honey 	28
30 	Steaks 	19
31 	Scallops 	22

Use a JOIN, like

JOIN this table again ON this table's parentid = the joined table's id

and include this table again.name as one of the fields you retrieve. Throw in a few aliases while you're at it.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.