2009-08-11 10 views
0

j'ai une table ayantcomment sql retourner seulement toutes les entrées dupliquées

create table test(id int not null primary key, day date not null); 

insert into test(id, day) values(1, '2006-10-08'); 
insert into test(id, day) values(2, '2006-10-08'); 
insert into test(id, day) values(3, '2006-10-09'); 

select * from test; 
+----+------------+ 
| id | day  | 
+----+------------+ 
| 1 | 2006-10-08 | 
| 2 | 2006-10-08 | 
| 3 | 2006-10-09 | 
+----+------------+ 


select day, count(*) from test GROUP BY day; 
+------------+----------+ 
| day  | count(*) | 
+------------+----------+ 
| 2006-10-08 |  2 | 
| 2006-10-09 |  1 | 
+------------+----------+ 


select day, count(*) from test group by day HAVING count(*) > 1; 
+------------+----------+ 
| day  | count(*) | 
+------------+----------+ 
| 2006-10-08 |  2 | 
+------------+----------+ 

Ce que mon besoin est, je dois retourner les entrées en double

C'est simple eteint que je besoin

+------------+----------+ 
| day  | id | 
+------------+----------+ 
| 2006-10-08 |  2 | 
| 2006-10-08 |  1 | 
+------------+----------+ 

Répondre

6

Essayez un autojointure

SELECT T1.day, T1.id 
FROM test T1 
INNER JOIN test T2 
ON T1.id <> T2.id AND T1.day = T2.day 
0
select id, count(day) as cnt from test group by day HAVING cnt > 1; 
Questions connexes