How to select parent row only if has at least one child?
- by Matt McCormick
I have a simple one-to-many relationship. I would like to select rows from the parent only when they have at least one child. So, if there are no children, then the parent row is not returned in the result set.
Eg.
Parent:
+--+---------+
|id| text |
+--+---------+
| 1| Blah |
| 2| Blah2 |
| 3| Blah3 |
+--+---------+
Children
+--+------+-------+
|id|parent| other |
+--+------+-------+
| 1| 1 | blah |
| 2| 1 | blah2 |
| 3| 2 | blah3 |
+--+------+-------+
I want the results to be:
+----+------+
|p.id|p.text|
+----+------+
| 1 | Blah |
| 2 | Blah2|
+----+------+