MySQL Subquery (or join?) to Select value of another tables ID -
i have taken @ following answer: select rows table row in table same id has particular value in column
which did ok job in confusing me far sub-queries.
my table looks this:
field type ------ ------------ id int(11) parent int(11) title varchar(255) uri varchar(255) perms varchar(255)
what trying do, select records , show parent , parent_name (if parent exists.
so example output, if had 2 rows
foo id of 1 & bar id of 2 , child of 1
foo null bar foo 1
my sql looks this:
select id,parent,title,uri,perms ls_menu_manager parent in ( select title parent_name ls_menu_manager parent=id )
but not work intended. think have gone , confused myself appreciated.
it looks me should try using left join
select ch.id, ch.parent, ch.title, ch.uri, ch.perms, pr.title parent_name ls_menu_manager ch left join ls_menu_manager pr on pr.id = ch.parent
this display rows in ls_menu_manager, , parent name when 1 present.
if no parent defined particular row, parent_name show null.
if want rows have parent, change left join natural join, removing left keyword.
select ch.id, ch.parent, ch.title, ch.uri, ch.perms, pr.title parent_name ls_menu_manager ch join ls_menu_manager pr on pr.id = ch.parent
Comments
Post a Comment