繁体   English   中英

如何Sql仅返回所有重复的条目

[英]how to Sql return only all duplicated entries

我有一张桌子

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 |
+------------+----------+

我需要的是,我需要返回重复的条目

这是我需要的简单输出

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

尝试自我加入

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM