如何 找出表中两个字段重复(同单位同名)的记录

sh5281 2005-11-25 01:25:48
如下表:person
PersonName, PersonDept, PersonType, ....

如何将表中 相同单位,而且姓名相同的人员 记录给搜索出来。
...全文
190 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
funsuzhou 2005-11-25
  • 打赏
  • 举报
回复
select a.* from person a
right join (select PersonName,PersonDept from person group by PersonName,PersonDept having count(*)>1)b
on a.PersonName=b.PersonName and a.PersonDept=b.PersonDept
sh5281 2005-11-25
  • 打赏
  • 举报
回复


好了,多谢楼上各位的答复,多谢了。
yongwin 2005-11-25
  • 打赏
  • 举报
回复
select *,distinct(PersonName,PersonDept) from person a left join person
on a.PersonName=PersonName where a.PersonDept=PersonDept
love16 2005-11-25
  • 打赏
  • 举报
回复
create table aa (PersonName varchar(20), PersonDept varchar(20), PersonType varchar(20))

insert into aa
select '李四','财务','11'union all
select '李三','采购','12'union all
select '李四','财务','13'union all
select '李五','财务','14'union all
select '李一','采购','15'union all
select '李三','采购','16'

select 别名,count(别名) 重复个数 from
(select (rtrim(PersonName)+rtrim(PersonDept)) as 别名 from aa) t group by 别名 having count(别名)>1

结果:

李三采购 2
李四财务 2


点点星灯 2005-11-25
  • 打赏
  • 举报
回复
--没测试

select a.* from person a,person
where a.PersonDept=PersonDept and a.PersonName=PersonName
love16 2005-11-25
  • 打赏
  • 举报
回复
select 别名,count(别名) from (select (rtrim(PersonName)+rtrim(PersonDept)) as 别名 from 表名) group by 别名 having count(别名)>1
zlp321002 2005-11-25
  • 打赏
  • 举报
回复
->2楼还真快,我说我自己。:)
love16 2005-11-25
  • 打赏
  • 举报
回复
select 别名,count(别名) from (select (rtrim(PersonName)+rtrim(PersonDept)) as 别名 from 表名) group by 别名 having count(别名)>2
zlp321002 2005-11-25
  • 打赏
  • 举报
回复
--上面有点问题,没有标识唯一主键,这样试下/

select * from @t A
where
(select count(*) from @t where a=A.a and b=A.b)>1
Andy__Huang 2005-11-25
  • 打赏
  • 举报
回复
select PersonName, PersonDept,num=count(*)
from person
group by PersonName, PersonDept
having count(*)>1
zlp321002 2005-11-25
  • 打赏
  • 举报
回复
select
*
from 表 A
where exists
(select 1 from 表 where PersonName=A.PersonName and PersonDept=A.PersonDept)

34,576

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧