Relate named ID in the same table

2

I need some help.

I have a category table that has the fields:

  • id;
  • name;
  • description;
  • id_category_name;
  • id_user;

I want to get the category name when the category_id id is equal to the category id.

Example:

The category Car has the father category Vehicles id = 4; name = Car; description = any; id_categoria_pai = 1 (vehicles); id_user = 1;

I need to display the name of the parent category, in this case, Vehicle.

How can I do this? I tried it anyway and I can not.

Or do I create the separate tables? I do not think so.

Thank you!

    
asked by anonymous 18.02.2015 / 12:08

1 answer

2

Looking at what the user provided information (parent categories and daughters are in the same table) the solution to the case would be more or less the following:

SELECT * FROM categorias as cat1 LEFT JOIN categorias as cat2 ON cat2.id = cat1.id_categoria_pai WHERE cat1.id=1;

But the interesting thing would be to separate the tables in categories and subcategories for better maintenance and understanding;

    
18.02.2015 / 12:25