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