小毅总结之--->sql语句查询出重复记录

Posted on 2008-11-09 11:24 H2O 阅读(565) 评论(1)  编辑  收藏 所属分类: DataBase sql语句查询出重复记录" trackback:ping="http://www.blogjava.net/xiaoyi/services/trackbacks/239506.aspx" /> -->

-- 建表
create table a(
id int primary key auto_increment,
contents varchar(32)
);
--插入测试数据
insert into a values(default,'AA');
insert into a values(default,'BB');
insert into a values(default,'AA');
insert into a values(default,'CC');
insert into a values(default,'BB');
insert into a values(default,'DD');
insert into a values(default,'EE');
insert into a values(default,'FF');
insert into a values(default,'FF');
-- 查出所有重复数据
select id,contents from (
    select * from (select  * from a) aa
        inner join
            (select id idd,contents cons from a) bb
                where aa.id!=bb.idd and aa.contents = bb.cons
) rst order by id asc;

-- 查处重复数据中的一条 如:AA BB FF重复了 只查处一个AA 一个BB 一个FF
select distinct(contents)  from (
select   id,contents  from (
    select * from (select  * from a) aa
        inner join
            (select id idd,contents cons from a) bb
                where aa.id!=bb.idd and aa.contents = bb.cons
) rst order by id asc
) t;

 其他方法1:

select aa.id,aa.contents from a as aa,(select contents from a group by contents HAVING count(*)>1) bb
where aa.contents = bb.contents 

 其他方法2:
select contents from a as aa where (select count(contents) from a as b where aa.contents = b.contents)>1

Feedback

# re: 小毅总结之--->sql语句查询出重复记录  回复  更多评论   

2011-10-10 16:09 by ian
一个人有两条重复的记录 数据库里现在有几万条这样重复的记录 现在我要删除id号大那个重复记录 也就是只留一条id号小的一条 查询语句怎么写出来呢?谢谢!!

只有注册用户登录后才能发表评论。


网站导航:
 

posts - 0, comments - 21, trackbacks - 0, articles - 101

Copyright © H2O