I have 2 table with this sample data.
Parent
id | title |
---|---|
1 | A |
2 | B |
3 | C |
Childs
id | p_id | number |
---|---|---|
1 | 1 | 1 |
2 | 1 | 2 |
3 | 1 | 3 |
4 | 2 | 4 |
5 | 2 | 5 |
6 | 2 | 6 |
7 | 3 | 2 |
8 | 3 | 7 |
9 | 3 | 8 |
10 | 3 | 9 |
And I want to get rows from parents join with childs and number > 3.
But I want to receive only parent whose condition is correct on all childs, and even if the condition is not correct on one child, the parent should not be returned
And I want to do without subquery
SELECT * FROM `parent`
LEFT JOIN `childs` on `childs`.`p_id` = `parent`.`id`
WHERE `childs`.`number` > '3'
I want to get only parent B with this condition.
Thanks.
3
Answers
Maybe something like this can work
Try this:
Here’s a fiddle demo
I would recommend:
There is no reason at all to use strings for what you want to do. It just confuses the logic.
An even more efficient method would use
NOT EXISTS
: