So basically I have a table called folders, which store data which I use to build up a file structure on a website, now one of the limits I wish to impose is only allowing the user to make 3 folders deep, as in they can only make 3 folders in like so:
Folder 1
File 1.1
>
Folder 2
File 2.1
>
Folder 3
File 3.1
Hopefully that visualises how far I want to go.
The table has: folderID
, folderName
and parentFolderID
.
parentFolderID
is set to NULL
if that folder does not below to another folder, if a folder does belong to another folder then parentFolderID
is set to another folders folderID
.
How could I use SQL to check how deep a user has gone already, so that I can return an error and say they have reached they maximum folder depth? Is this sort of thing possible in SQL or should I process this backend?
UPDATE:
Since using information from here: http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql
I came up with this:
SELECT node.folderName, (COUNT(parent.folderID))
FROM folders AS node,
folders AS parent
WHERE node.folderID BETWEEN parent.parentFolderID AND parent.folderID
GROUP BY node.folderName
ORDER BY node.folderName;
But when it checks on a folder which is the third one deep, it only returns 1 as what was counted when it should be 3? What am I doing wrong?