我有一个连接表,其中包含两列的组合主键。 我想查询所有条目
columnA having count > 1
和
columnB = value1 and value2
到目前为止我的查询看起来像这样
select
columnA
from tableA
where columnB = 1 and
columnA in (
select
columnA
from tableA
group by columnA
having count(columnA) > 1)
或
select
columnA
from tableA
where columnB = 2 and
columnA in (
select
columnA
from tableA
group by columnA
having count(columnA) > 1)
我该如何查询
...columnB = 1 and columnB = 2 and columnA in (select ....
答案 0 :(得分:2)
select columnA
from tableA
where columnB in (1,2)
group by columnA
having count(distinct columnB) = 2
自动1
填充2
和columnB
两个值count(columnA) > 1
如果您希望 columnB
条件(columnB有2个值)或 columnA
条件,那么
select columnA
from tableA
group by columnA
having count(*) > 1
or count(distinct columnB) = 2
或columnB
必须为1
和2
select columnA
from tableA
group by columnA
having count(*) > 1
or
(
sum(case when columnB = 1 then 1 else 0 end) > 0 and
sum(case when columnB = 2 then 1 else 0 end) > 0
)