Welcome to WebmasterWorld Guest from 54.205.119.93

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

sql issue - need table join

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

WebmasterWorld Senior Member 10+ Year Member



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)

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



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)

WebmasterWorld Senior Member 10+ Year Member



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)

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



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
 

Featured Threads

Hot Threads This Week

Hot Threads This Month