如何从多对多 table 中获取这些行,其中在确切的列中包含 0?
How to get these rows from many to many table, which contains 0 in exact column?
a|a_id|b_id
1| 13 | 0
2| 14 | 0
3| 13 | 57
4| 13 | 0
5| 15 | 11
6| 15 | 0
7| 15 | 234
8| 14 | 0
所以我只想 return 这些 a_id
,其中 b_id
中的所有行都是 0。在这种情况下,它将是 a_id = 14.
a|a_id|b_id
1| 13 | 0
2| 14 | 0
3| 13 | 57
4| 13 | 0
5| 15 | 0
6| 15 | 0
7| 15 | 0
8| 14 | 0
在第二种情况下它将是 14 和 15
我尝试了什么:
select a_id
from id_table
group by a_id
having max(b_id) > 0
您可以使用聚合,并使用 having
子句进行过滤:
select a_id
from mytable
group by a_id
having max(b_id) = 0
这假定 b_id
中没有负值。否则你可以这样做:
having max(b_id <> 0) = 0
如果你想要完整的行,那么你可以使用 not exists
:
select t.*
from table t
where not exists (select 1 from table t1 where t1.a_id = t.a_id and t1.b_id <> 0);
a|a_id|b_id
1| 13 | 0
2| 14 | 0
3| 13 | 57
4| 13 | 0
5| 15 | 11
6| 15 | 0
7| 15 | 234
8| 14 | 0
所以我只想 return 这些 a_id
,其中 b_id
中的所有行都是 0。在这种情况下,它将是 a_id = 14.
a|a_id|b_id
1| 13 | 0
2| 14 | 0
3| 13 | 57
4| 13 | 0
5| 15 | 0
6| 15 | 0
7| 15 | 0
8| 14 | 0
在第二种情况下它将是 14 和 15
我尝试了什么:
select a_id
from id_table
group by a_id
having max(b_id) > 0
您可以使用聚合,并使用 having
子句进行过滤:
select a_id
from mytable
group by a_id
having max(b_id) = 0
这假定 b_id
中没有负值。否则你可以这样做:
having max(b_id <> 0) = 0
如果你想要完整的行,那么你可以使用 not exists
:
select t.*
from table t
where not exists (select 1 from table t1 where t1.a_id = t.a_id and t1.b_id <> 0);