如何从多对多表中获取这些行,该表的精确列中包含0?

4xrmg8kj  于 2021-08-13  发布在  Java
关注(0)|答案(2)|浏览(270)
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

所以我只想退回这些 a_id 所有的行 b_id 是0。在这种情况下,它将是一个\u 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
2admgd59

2admgd591#

您可以使用聚合,并使用 having 条款:

select a_id
from mytable
group by a_id
having max(b_id) = 0

这假设在中没有负值 b_id . 否则你可以:

having max(b_id <> 0) = 0
lnxxn5zx

lnxxn5zx2#

如果您想要整行,那么您可以使用 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);

相关问题