-1
记录具有以下表中数据要检索只有两个特定值
Example Table
ID Value
1 a
1 b
1 c
2 a
2 b
2 c
3 a
3 b
我需要检索具有只有两个值a和b的ID记录。 所以我期待只有ID 3的记录。 谁能帮我查询
记录具有以下表中数据要检索只有两个特定值
Example Table
ID Value
1 a
1 b
1 c
2 a
2 b
2 c
3 a
3 b
我需要检索具有只有两个值a和b的ID记录。 所以我期待只有ID 3的记录。 谁能帮我查询
我想你可以做这样的事情
select
ID,
sum(case when value = 'a' then 1
when value = 'b' then 1
else 3 end)
from
table1
group by id
having
sum (case when value = 'a' then 1
when value = 'b' then 1
else 3 end) =2
,将工作:
select x.id from
(
select id from mytable where value = 'a'
union all
select id from mytable where value = 'b'
) x
group by x.id
having COUNT(*) = 2
and not exists (select * from mytable t where t.id = x.id and value <> 'a' and value <> 'b')
请更精确地说明这个问题。到底什么意思是“两个值A和B”?各一个?两个在任何组合? – WarrenT