繁体   English   中英

MySQL查询按发生顺序从映射表返回结果

[英]Mysql query to return result from mapping table in order of occurence

我有一个表A_B映射。 现在我只需要那些与单个B相关联的A。

A    B
12  16
12  22
12  23
12  26
23  16
24  26

假设如果我搜索与B = 16关联的A,我将得到12和23A。但是我只想要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)

你是这个意思吗?

Select A FROM A_B where B IN (Select B FROM A_B where A=12) and A<>'12'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM