我有一个表 A_B 映射。现在我想要那些仅与单个 B 关联的 A。
A B
12 16
12 22
12 23
12 26
23 16
24 26
假设如果我搜索与 B = 16 相关的 A,我将得到 12 和 23 个 A。 但我只想要 23,因为它只与 B=16 相关。
第二个选择可以是前 23 个,然后将出现 12 个。 因此,首先会优先处理单个关联项,然后才会发生多个关联。
最佳答案
1.
select A
from test4 T1
where B=16
and not exists(select 1 from test4 T2
where T2.A=T1.A and T2.B<>T1.B)
2.
select A
from test4 T1
where B=16
order by exists(select 1 from test4 T2
where T2.A=T1.A and T2.B<>T1.B)
关于Mysql查询从映射表中按出现顺序返回结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39122773/