php&mysql find parent category name -


i have problem inner join, left join commands.

my category table is:

 id  | parent | title 1   | 0      | first category 2   | 1      | other category 

i have list categorys , want parents category title @ sql command.

i have tried:

select cat.id, cat.title, cat2.title parentcatname, cat.parent   categories cat inner join categories cat2 on cat2.id=cat.parent 

but ıt's not working.

you have use left join able pull categories no matter have parent category or not. inner join filters out mismatches.

select c.id, c.title, c.parent, p.title parent_title    categories c left join categories p      on c.parent = p.id 

output:

 | id |          title | parent |   parent_title | ------------------------------------------------- |  1 | first category |      0 |         (null) | |  2 | other category |      1 | first category | 

here sqlfiddle demo


Comments