SQL Query选择所有子文件夹

时间:2013-12-14 11:33:03

标签: mysql sql

所以我有这两张桌子;

文件夹

  • folderID
  • 名称

folderFolder

  • 帐户ID
  • parentFolderID
  • childFolderID

folderFolder基本上是一个链接表,用于链接子文件夹和文件夹。现在我基本上要做的是选择链接到所选文件夹的所有子文件夹。

假设我有这个文件夹层次结构;

First Folder (this folder has id of 112)
  - Second Sub Folder 1
      - Third Sub folder of Second Sub Folder 1
  - Second Sub Folder 2

我希望获得包含所有子文件夹的第一个文件夹..我这样做直到现在但它只获得第二个子文件夹

SELECT * 
FROM folderFolder ff 
INNER JOIN folder f 
ON f.folderID = ff.childFolderID 
WHERE ff.ParentFolderID='112'

任何帮助将不胜感激

2 个答案:

答案 0 :(得分:1)

再次尝试加入:

SELECT *
FROM folderFolder ff 
INNER JOIN folderFolder ff_child
ON ff_child.parentFolderID = ff.childFolderID
INNER JOIN folder f
ON f.folderID = ff.childFolderID OR f.folderID = ff_child.childFolderID
WHERE ff.parentFolderID='112'
如果parentFolderID是数字,则不应引用BTW'112'。

答案 1 :(得分:0)

试试这个,它将获取任何级别的子目录..

select parentFolderId, childFolderId, @parentFolderId:=childFolderId curr_child, f.folderId, f.name from folderFolder ff inner join folder f on f.folderId=ff.childFolderId;