tonelm54
asked on
Count of ParentFolders
Im trying to create a table of folders which I would like to be able to see if there is any children folders below.
I have a table with 3 coloums:-
The ParentID has the value of folderID of its parent.
And I query it simply like
However I want it to be able to count the number of folders that have the parentID the same as the folderID, so something like:-
I know I need to make a subquery, but Im unsure how to do this, does anyone have any advice?
I have a table with 3 coloums:-
folderID, ParentID, Folder
The ParentID has the value of folderID of its parent.
And I query it simply like
Select * from Folder
However I want it to be able to count the number of folders that have the parentID the same as the folderID, so something like:-
Select *, (Countif(`parentID`=`folderID`)) from Folders;
I know I need to make a subquery, but Im unsure how to do this, does anyone have any advice?
select count(*) from Folder where folderid=parentId
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I gueess you have @folderid, and you want the count of records/folders in same parent
select count(*) from Folder where parentId = (select parentid from folder where folderid=@folderid)
select count(*) from Folder where parentId = (select parentid from folder where folderid=@folderid)
ASKER
Ok, so Ive put in:-
However it is always returning 0
Just as an example, One of my rows has
If I do
I get 5 rows returned
SELECT * , (SELECT count(*) FROM Folder WHERE `ParentID` =`folderID`) As subFolders
FROM`Folder`
However it is always returning 0
Just as an example, One of my rows has
folderID = 1842-40090-42653
parentID = 83770-6566-264
If I do
SELECT* FROM`Folder` WHERE`ParentID` LIKE '83770-6566-264';
I get 5 rows returned
Then try
select parentID, count(*) from Folder group by parentID ?
select parentID, count(*) from Folder group by parentID ?
Add parentid = @parentid if you meant that
try:
SELECT a.* , (SELECT count(*) FROM Folder b WHERE b.ParentID =a.folderID) As subFolders
FROM Folder a
SELECT a.* , (SELECT count(*) FROM Folder b WHERE b.ParentID =a.folderID) As subFolders
FROM Folder a