1
發現親子樹我有一個表child
在那裏我有以下結構:如何從SQL表
id child_name parent_id parent_name
1 vikas 2 sunny
2 john 3 seema
3 ajay 4 delhi
4 josh 4 main
我試圖找到孩子父母,直到PARENT_NAME匹配爲主,那麼我就會停下來。如果可能的話,我需要單個查詢sql。
我試過這個,但它只給我一個級別。
SELECT ChildUserType.child_name,
ChildUserType.parent_name,
ParentUserType.id,
ParentUserType.parent_id
FROM groups AS ChildUserType
LEFT JOIN groups AS ParentUserType
ON ChildUserType.id = ParentUserType.parent_id
where ChildUserType.id='1';
能否請你告訴你有什麼到目前爲止已經試過?在SO,我們想幫助解決技術問題或類似問題,我們不能從頭開始提供代碼。請閱讀我們的[如何問問指南](http://stackoverflow.com/help/how-to-ask)以獲取更多詳細信息 – dubes
我正在用我的答案編輯此內容。 –
我想通過哪裏id = 1,然後試圖找到所有的孩子,其中id = 1。 –