Link to home
Start Free TrialLog in
Avatar of hankknight
hankknightFlag for Canada

asked on

MySQL: limit results to items with at least one child item

With MySQL, I want to limit results to items with at least one child item.

My table has a column named `ParentID` and I only want results where at least one item uses the item returned as its `ParentID`

SELECT `Name`,`ID` FROM `File` WHERE `ClassName` = 'Folder' AND  (there is at least one `File` with the `ParentID` of this `ID`)

Open in new window

SOLUTION
Avatar of hielo
hielo
Flag of Wallis and Futuna image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hankknight

ASKER

Thank you both.  hielo, I modified your code to make it work.  

wpcortes, your code works as is.

SELECT `Name`,`ID` FROM `File` WHERE `File`.`ClassName` = 'Folder' 
AND `File`.`ID` IN (SELECT `f2`.`ParentID` FROM `File` as `f2` WHERE `f2`.`ParentID`=`File`.`ID`)

Open in new window