MySQL Query Ebeveyn Çocuk

0 Cevap php

I am working with mysql and php and have a Parent Child table created in mysql. Every parent has 3 Childs. Every Child becomes also a Parent and has also 3 Childs and so on.

Şimdi bana 3 Childs yok (seçilen üst kimliği bağlı) ağacında son üst veren bir fonksiyon yazmak gerekir. Yani önümüzdeki Çocuk abonelik o ebeveyne adanmış olabilir.

Bu mysql sorgu veya fonksiyonu ile yapılabilir mi?

Thanks, M


Ben aşağıdaki sorguyu kullanabilirsiniz:

SELECT t1.uid, t2.pid FROM cms_users AS t1 LEFT JOIN cms_users AS t2 ON t1.uid = t2.pid 

Hangi mr aşağıdaki çıktıyı verir:

t1.uid t2.pid
1      1 
1      1 
1      1 
2      2 
2      2 
2      2 
3      3 
4      NULL 
5      NULL

Ne aslında ihtiyaç gibi bir sonucudur:

p1.uid p2.pid 
1      3 
2      3 
3      1 
4      0 
5      0 

This result also starts from the root Parent 1 I need to get the results starting from a selected uid somewhere in the three. Every parent has his own tree starting from his uid. Probably I need to write a stored procedure or something but this is all new to me and I don't know how to do this.

This is an example of how the tree looks like. http://www.musafreaks.com/images/tree.jpg User ID 1 has his own tree, even user 2, 3 and 4 have there own tree and so on.

0 Cevap