I’ve two tables as following.
x
Table A
Column Id | Column Name
1 | Test 1
2 | Test 2
The relationship is
Table B
Column Id | Column Delivered | Column TableA_Id
1 | true | 1
2 | true | 1
3 | true | 1
4 | true | 1
5 | false | 2
6 | true | 2
7 | true | 2
What I want is to return for example for table A the record with id 1 which has a relationship with table B where all the values on table B column Delivered as true.
Advertisement
Answer
You can use group by
and having
:
select tablea_id
from b
group by tablea_id
having sum( delivered <> 'true' ) = 0;
This doesn’t use table a. If you want all the columns there, you can also use;
select a.*
from a
where not exists (select 1
from b
where b.tablea_id = a.id and b.delivered <> 'true'
);
The one difference with this query is that it will also return rows in a
that have no rows in b
at all.