请教高手如何用SQL语言查询出表中的重复行?我所查的不是关键字数据,是此表中其它一个字段的数据有重复,需要查出。多谢!
如果光选择重复行的话
可以这样
select distinct 有重复内容的字段名 from 表名 group by 有重复内容的字段名 having count(有重复内容的字段名)>1
这样只是选出有重复的记录的字段
如果要全部取出来的话
用这样的语句
select * from 表名 where 有重复内容的字段名 in (select distinct 有重复内容的字段名 from 表名 group by 有重复内容的字段名 having count(有重复内容的字段名)>1 )
即可
例如:
create table users
(
fseq number(8),
user_name varchar2(20),
address varchar2(40),
fpassword varchar2(12)
)
alter table users
add(
primary key(fseq)
)
select t.*,t.rowid from users t
select distinct 有重复内容的字段名 from 表名 group by 有重复内容的字段名 having count(有重复内容的字段名)>1
select user_name from users where user_name in
(select distinct user_name from users group by user_name having count(user_name)>1)
select I.* from users I where exists
(select distinct user_name,fpassword from users
where User_Name = I.User_Name and fpassword = I.Fpassword group by user_name having count(*)>1 ))
创建临时表查询
with tbl as(
select I.User_Name,i.address,i.fpassword from users I where exists
(select distinct user_name,fpassword from users
where User_Name = I.User_Name and fpassword = I.Fpassword group by user_name having count(*)>1 )
) select distinct * from tbl b
1 lizhitao dgfd 123 AAAHZjAABAAAMYyAAA
2 lizhitao dgfd 123 AAAHZjAABAAAMYyAAB
3 fd dfd 323 AAAHZjAABAAAMYyAAC
4 fdf fd 5354 AAAHZjAABAAAMYyAAD
5 dd dsfd 23 AAAHZjAABAAAMYyAAE
6 dd sfd 23 AAAHZjAABAAAMYyAAF
7 lizhitao gh 755 AAAHZjAABAAAMYyAAG
如果光选择重复行的话
可以这样
select distinct 有重复内容的字段名 from 表名 group by 有重复内容的字段名 having count(有重复内容的字段名)>1
这样只是选出有重复的记录的字段
如果要全部取出来的话
用这样的语句
select * from 表名 where 有重复内容的字段名 in (select distinct 有重复内容的字段名 from 表名 group by 有重复内容的字段名 having count(有重复内容的字段名)>1 )
即可
例如:
create table users
(
fseq number(8),
user_name varchar2(20),
address varchar2(40),
fpassword varchar2(12)
)
alter table users
add(
primary key(fseq)
)
select t.*,t.rowid from users t
select distinct 有重复内容的字段名 from 表名 group by 有重复内容的字段名 having count(有重复内容的字段名)>1
select user_name from users where user_name in
(select distinct user_name from users group by user_name having count(user_name)>1)
select I.* from users I where exists
(select distinct user_name,fpassword from users
where User_Name = I.User_Name and fpassword = I.Fpassword group by user_name having count(*)>1 ))
创建临时表查询
with tbl as(
select I.User_Name,i.address,i.fpassword from users I where exists
(select distinct user_name,fpassword from users
where User_Name = I.User_Name and fpassword = I.Fpassword group by user_name having count(*)>1 )
) select distinct * from tbl b
1 lizhitao dgfd 123 AAAHZjAABAAAMYyAAA
2 lizhitao dgfd 123 AAAHZjAABAAAMYyAAB
3 fd dfd 323 AAAHZjAABAAAMYyAAC
4 fdf fd 5354 AAAHZjAABAAAMYyAAD
5 dd dsfd 23 AAAHZjAABAAAMYyAAE
6 dd sfd 23 AAAHZjAABAAAMYyAAF
7 lizhitao gh 755 AAAHZjAABAAAMYyAAG