sql如何保留重复行,祛除不重复数据

来源:百度知道 编辑:UC知道 时间:2024/09/21 15:24:15
我取的表是要保留ID这个行的重复数据,但是取出的有不重复的数据,怎么样才能取出我想要的表?请给我详细答案包括原理

select id,count(*) from table1 group by id having count(*)>1;

原理是:对表的ID字段进行分组,并查询各不同ID记录数.通过ID记录总数大于1的条件进行过滤.

select a.*
from table1 a, (select id,count(*)
from table1
group by id
having count(*)>1) b
where a.id = b.id;

select id,count(*) from table1 group by id having count(*)>1;