beyzad Posted February 7, 2015 Share Posted February 7, 2015 Hi there. I have a table named `cat` that looks like this: id |name |parent --------+---------------+------ 1 |cat1 |0 --------+---------------+------ 2 |cat2 |0 --------+---------------+------ 3 |subcat1 |1 --------+---------------+------ 4 |subcat2 |1 --------+---------------+------ 5 |subsubcat1 |3 --------+---------------+------ I need to export something like this with only 1 query if it is possible: id |name |parent(name) --------+---------------+------------ 1 |cat1 |NULL --------+---------------+------------ 2 |cat2 |NULL --------+---------------+------------ 3 |subcat1 |cat1 --------+---------------+------------ 4 |subcat2 |cat1 --------+---------------+------------ 5 |subsubcat1 |subcat1 --------+---------------+------------ Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/294446-join-a-table-with-itself/ Share on other sites More sharing options...
Solution Barand Posted February 7, 2015 Solution Share Posted February 7, 2015 SELECT a.id , a.name as item , b.name as parent FROM cat AS a LEFT JOIN cat AS b ON b.parent = a.id Quote Link to comment https://forums.phpfreaks.com/topic/294446-join-a-table-with-itself/#findComment-1505161 Share on other sites More sharing options...
beyzad Posted February 7, 2015 Author Share Posted February 7, 2015 Thanks. I owe you :happy-04: Quote Link to comment https://forums.phpfreaks.com/topic/294446-join-a-table-with-itself/#findComment-1505162 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.