tags:

views:

36

answers:

3

my table contains category_name and parent_category_Id column

My parent_category data contains, the same table primary key id.

i need to select the all rows and insted my parent_category_Id i need to select the catogry_name

A: 

Try something like:

 SELECT c.category_name, p.category_name
  FROM categories c LEFT JOIN parent_categories p
  ON c.parent_id = p.id    

PS: you may think about restructuring your database, it would make more sense to store all the categories in the same table. See for instance: http://dev.mysql.com/tech-resources/articles/hierarchical-data.html

nico
A: 

I think this is what you're after, though it's hard to discern from the question:

Select c.*
From category c 
  Join parent_category pc ON c.parent_category_id = pc.id
Where pc.category_name = 'Some Name'
Nick Craver
its working but i am not able to get the parent row...parent row contains the row value as 0.
Ayyappan.Anbalagan
@Ayyappan.Anbalagan - Can you clarify a bit? If you want the data from the parent table as well, you can do `Select pc.*, c.*`, or better, select the columns you actually want...but I'm not sure from the question *exactly* what you're after.
Nick Craver
A: 

You should restructure your database to have one table with:

id, name, and parent columns, with the parent column referencing the same table's id column. Your current database is not normalized and will likely cause issues in the future.

At a minimum you should have an auto_increment id column in the categories table.

The other answers here are correct (depending on the SQL server you are using).

smdrager