我的表:
Col1 Col2
1 xyz
1 abc
2 abc
3 yyy
4 zzz
4 zzz
我有一个有两列的表。我想查询col1具有多个DISTINCT col2值的记录。在上面给出的示例表中,查询应返回值为“1”的col1的记录。
预期查询结果:
Col1 Col2
1 xyz
1 abc
答案 0 :(得分:4)
SELECT *
FROM tableName
WHERE Col1 IN
(
SELECT Col1
FROM tableName
GROUP BY Col1
HAVING COUNT(DISTINCT col2) > 1
)
答案 1 :(得分:1)
select t.col1, t.col2
from (
select col1
from tbl
group by col1
having MIN(col2) <> MAX(col2)
) x
join tbl t on t.col1 = c.col1