btwong Posted January 7, 2008 Share Posted January 7, 2008 People, i need help with a query. I could do this in 2 queries, but i don't want to do it, as it will be lazy, and has the potential to end up executing over 100 queries, when i could just do it in one... anyway my table looks like this: table1 id name link parent_id -------------------------------------------------------- 1 link1 link.php 0 2 link2 link2.php 0 3 Sublink1 link3.php 1 4 sublink2 link4.php 1 5 sublink3 link5.php 2 etc Know i want one query that will call all the links, but if it has a parent_id link of >1, add the parent links name (name) to come out in the query instead of the parent_id. So in essence the query will be calling itself. am i to use a left outer join, or inner join or something else??? please help as i am frustrated as hell trying to work this out. o yes, i am using this script on a PHP page with mysql. thanks Link to comment https://forums.phpfreaks.com/topic/84865-query-help/ Share on other sites More sharing options...
fenway Posted January 7, 2008 Share Posted January 7, 2008 Yes, you need a self-join to the same table (differnet) alias using t1.parent_id = t2.id in the on clause. Link to comment https://forums.phpfreaks.com/topic/84865-query-help/#findComment-432706 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.