0

I have Following structure in my table with the data

enter image description here

Now From Above Data let say if I want to find all sub organization where organization Id is given..then how can find that .

here how can i make it show Reseller A,Reseller B,Advertiser A,Advertiser B,when organization Id = 10707(Main Organization)

My table name is organization_ and there is

parentorganizationId

in each row which describes the parentorganization of that particular organization.

So when I give id of main organization say 10707 then it should return me all suborganizations with that organization id..

BenMorel
  • 34,448
  • 50
  • 182
  • 322
BhavikKama
  • 8,566
  • 12
  • 94
  • 164

2 Answers2

2

There are various models for Hierarchical data. It seems you are using both "Adjacency List" and "Path Enumeration" in your table. You can read about them in Bill Karwin's slideshow, which is discussing various models and implementations in MySQL: Models for Hierarchical data with SQL

So, using the treePath column, the query you need is rather simple:

SELECT
    GROUP_CONCAT(t.name) AS SubOrganizations
FROM
    tableX AS t
  JOIN
    tableX AS p
      ON  t.treePath LIKE CONCAT( p.treePath, '%') 
      AND t.organizationId <> p.organizationId 
WHERE
    p.organizationId = 10707 ;
ypercubeᵀᴹ
  • 113,259
  • 19
  • 174
  • 235
0

In short, unlike other RDMBS (SQL Server, Oracle), MySql doesn't support recursive queries.

Couple of helpful links on the subject for you from this site:

Community
  • 1
  • 1
peterm
  • 91,357
  • 15
  • 148
  • 157