Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a below table "User_Info". It carries all user info and the ID of the company that each user belongs to.
User_Id | User_Name | Company_Id
--------+-----------+--------------
1002    | User1     | 113
1003    | User2     | 114
1004    | User3     | 111

I have another table "Company" which has Company Information and its associated Parent Company.
id  | Company_Name    | Parent_Company_Id
----+-----------------+------------------
110 | WALMART         | NULL
111 | WALMART TEXAS   | 110
112 | WALMART DALLAS  | 111
113 | WALMART HOUSTON | 111
114 | WALMART KATY    | 113


How do I get the below output? The Company_id in the User_Info table should list all the hierarchical companies under that Company_id.
user_id  | company_id
---------+--------------
1002     | 113
1002     | 111
1002     | 110
1003     | 114
1003     | 111
1003     | 110
1004     | 111
1004     | 110


What I have tried:

left join and inner join. But it doesent work
Posted
Updated 10-May-16 17:25pm
v2

1 solution

MySQL doesn't support hierarchical queries, and if you have done any searching you will see many different solutions for this problem.

I think this question, and the answers, best match your case:
recursion - Recursive mysql select? - Stack Overflow[^]

Here is another similar question:
sql - How to create a MySQL hierarchical recursive query - Stack Overflow[^]

Sorry for not being able to provide a specific solution.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900