mysql - SQL query to get id of all elements in a tree -
can 1 suggest me how id of children of particular tree.
table structure :
table 1 : customer table 2 : hierarchy cid hid hid cid 1 1 1 null 2 2 2 null 3 3 3 2 4 4 4 3 5 5 5 2 6 6 6 5 7 7 7 7
table (customer) main element , table hierarchy maintain hierarchy of customer.
i have give cid 2
root customer(parent). have cid
child of cid 2
(given).
expected result of above scenario 2,3,4,5,6
(2 given , these id set customer ids). actual answer query should return.
i looked recursive query, procedure , join unable result.
applicable mysql, postgresql , sql server
you can use recursive queries sql server using cte
(common table expressions).
syntax follows:
with cte ( select id hierarchy id =2 union select id hierarchy child inner join cte parent on child.parentid=parent.id ) select * cte
read more here: http://technet.microsoft.com/en-us/library/ms186243(v=sql.105).aspx
Comments
Post a Comment