2016-10-18 201 views

回答

2

试试这个:

SELECT t1.* 
FROM mytable AS t1 
JOIN (
    SELECT a 
    FROM mytable 
    GROUP BY a 
    HAVING COUNT(DISTINCT b) > 1 
) AS t2 ON t1.a = t2.a 
0

你没有提到你的DBMS,但是在Oracle中,你可以这样做:

select a,b 
from (
    select a,b, 
     count(distinct b) over (partition by a) as num_b 
    from the_table 
) t 
where num_b > 1 

不幸的是,对于窗口函数,Postgres或SQL Server不支持distinct

相关问题