Column1|Column2
1 |a
1 |b
1 |c
2 |a
3 |b
4 |a
4 |b
...
我需要的是编写sql statetment来获取column1中所有实体,其中第2列的值为AND b。 所以在这种情况下结果将是:1,4
提前帮忙。答案 0 :(得分:0)
试试这个
select column1 from your_table
group by column1
having
max(case when column2='a' then 1 else 0 end)+
max(case when column2='b' then 1 else 0 end)=2
答案 1 :(得分:0)
试试这个:
SELECT * FROM Table T
WHERE EXISTS(
SELECT 'A' FROM Table Ta
WHERE T.column1 = Ta.column1
AND Ta.column2 = 'a'
)
AND EXISTS(
SELECT 'B' FROM Table Tb
WHERE T.column1 = Tb.column1
AND Tb.column2 = 'b'
)
答案 2 :(得分:0)
使用自我加入:
select column1
from mytable t1 join mytable t2 on(t1.column1 = t2.column1)
where t1.column1 = 'a'
and t2.column1 = 'b'