Welcome to WebmasterWorld Guest from 54.237.251.98

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

sql issue - need table join

     
11:23 pm on Apr 12, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 21, 2002
posts: 1542
votes: 0


I am trying to do a table join, but I am trying to join the same table on to itself.

I am not sure how this achieved, or if there is an easy way to do it.

so I have
SELECT `categories_id`,`categories_name`,`parent_id` FROM `categories`

But I want the results to map the category name on to the category ID, so the results show the category name, instead of ID.
Currently I have to do an extra request in the while loop
SELECT `categories_name` FROM `categories` WHERE `categories_id`=$subcat_id


Any suggestions? I am sure its really simple.
3:30 am on Apr 13, 2013 (gmt 0)

Moderator from US 

WebmasterWorld Administrator lifeinasia is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Dec 10, 2005
posts:5628
votes: 48


Kind of confused... You're already pulling the categories_name in the query. Why don't you just display it?
4:20 am on Apr 13, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 21, 2002
posts: 1542
votes: 0


But I need to match the category name on the the id
A sample row is like this
id cat_id name
1 0 Mercedes
2 1 A Class

And I want to pull
2 Mercedes A Class
5:43 am on Apr 13, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member topr8 is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:Apr 19, 2002
posts:3212
votes: 14


to join a table to itself you use aliases:

SELECT a.id,b.name,a.name FROM categories as a,categories as b WHERE a.cat_id=b.id
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members