homepage Welcome to WebmasterWorld Guest from 23.20.19.131
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
sql issue - need table join
whatson




msg:4564390
 11:23 pm on Apr 12, 2013 (gmt 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.

 

LifeinAsia




msg:4564416
 3:30 am on Apr 13, 2013 (gmt 0)

Kind of confused... You're already pulling the categories_name in the query. Why don't you just display it?

whatson




msg:4564417
 4:20 am on Apr 13, 2013 (gmt 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

topr8




msg:4564428
 5:43 am on Apr 13, 2013 (gmt 0)

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

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved