2014-01-10 2 views
0

4 개의 열이있는 테이블이 있습니다. 1, 2 및 3 열의 행을 선택하고 싶습니다. 동일한 데이터가 있습니다. 테이블의 여러 열에서 중복 행 선택

TableName 
Col1 Col2 Col3 Col4 
abc 12 xyz  1 
abc 12 xyz  2 
abc 12 xyz  3 
abc 12 xyz  4 
def 34 wxy  5 
ghi 45 uvw  6 
ghi 45 uvw  7 
ijk 45 tuv  8 

나는 1

4와 6,7 감사 행을 선택 할 것으로 예상하고있다.

+0

[다음은 sqlfiddle] (http://sqlfiddle.com/#!4/37c04)입니다. – eebbesen

답변

2

당신이 그런 값 단지 목록을 얻으려고 노력하는 경우 :

select col1, col2, col3 
from tablename 
group by col1, col2, col3 
having count(*) > 1; 

당신이 실제 행을 원하는 경우, 다음이 다시 가입 : 그것이 일을

select tn.* 
from tablename tn join 
    (select col1, col2, col3 
     from tablename 
     group by col1, col2, col3 
     having count(*) > 1 
    ) tncol 
    on tn.col1 = tncol.col1 and tn.col2 = tncol.col2 and tn.col3 = tncol.col3; 
2

한 가지 가능한 방법을 다음과 같이

select distinct t1.* 
from TableName t1 
left join TableName t2 
on t1.Col4 <> t2.Col4 and t1.Col1 = t2.Col1 and t1.Col2 = t2.Col2 and t1.Col3 = t2.Col3 
where 
t2.Col4 is not null 

(테이블에 많은 행과 중복이 많이있는 경우) 이것은별로 좋지 않습니다.

관련 문제