在 TABLE表 中如何列出同名的数据
自动编号 员工ID 姓名 地点 出生年月
1 0001 AA JS 08-7-2
2 0004 AB CS 03-6-5
3 1234 DF JS 01-2-6
4 2341 AA cd 03-4-6
5 1231 CF JS 03-6-7
。。
如何将同名的数据筛选出来呢
效果如下:
自动编号 员工ID 姓名 地点 出生年月
1 0001 AA JS 08-7-2
4 2341 AA cd 03-4-6
自动编号 员工ID 姓名 地点 出生年月
1 0001 AA JS 08-7-2
2 0004 AB CS 03-6-5
3 1234 DF JS 01-2-6
4 2341 AA cd 03-4-6
5 1231 CF JS 03-6-7
。。
如何将同名的数据筛选出来呢
效果如下:
自动编号 员工ID 姓名 地点 出生年月
1 0001 AA JS 08-7-2
4 2341 AA cd 03-4-6
where 姓名 in(select 姓名 from tb group by 姓名 having count(*)>1)
-- Author:Flystone
-- Version:V1.001
-- Date:2008-09-02 13:20:03
-------------------------------------- Test Data: ta
If object_id('ta') is not null
Drop table ta
Go
Create table ta(自动编号 int,员工ID nvarchar(4),姓名 nvarchar(2),地点 nvarchar(2),出生年月 smalldatetime)
Go
Insert into ta
select 1,'0001','AA','JS','08-7-2' union all
select 2,'0004','AB','CS','03-6-5' union all
select 3,'1234','DF','JS','01-2-6' union all
select 4,'2341','AA','cd','03-4-6' union all
select 5,'1231','CF','JS','03-6-7'
Go
--Start
Select *
from ta a
where exists(select 1 from ta where a.姓名 = 姓名 and 自动编号 <> a.自动编号)
--Result:
/*
自动编号 员工ID 姓名 地点 出生年月
----------- ---- ---- ---- ------------------------------------------------------
1 0001 AA JS 2008-07-02 00:00:00
4 2341 AA cd 2003-04-06 00:00:00(所影响的行数为 2 行)*/
--End
if object_id('tempdb.dbo.#T') is not null drop table #T
create table #T (自动编号 int,员工ID varchar(4),姓名 varchar(2),地点 varchar(2),出生年月 datetime)
insert into #T
select 1,'0001','AA','JS','08-7-2' union all
select 2,'0004','AB','CS','03-6-5' union all
select 3,'1234','DF','JS','01-2-6' union all
select 4,'2341','AA','cd','03-4-6' union all
select 5,'1231','CF','JS','03-6-7'select * from #T
where 姓名 in(select 姓名 from #T group by 姓名 having count(*)>1)/*
自动编号 员工ID 姓名 地点 出生年月
----------- ---- ---- ---- ------------------------------------------------------
1 0001 AA JS 2008-07-02 00:00:00.000
4 2341 AA cd 2003-04-06 00:00:00.000(所影响的行数为 2 行)
*/
if object_id('tb') is not null
drop table tb
go
create table tb(自动编号 int identity(1,1),员工ID varchar(10),姓名 varchar(10),地点 varchar(10),出生年月 varchar(10))
go
insert into tb
select '0001','AA','JS','08-7-2' union all
select '0004','AB','CS','03-6-5' union all
select '1234','DF','JS','01-2-6' union all
select '2341','AA','CD','03-4-6' union all
select '1231','CF','JS','03-6-7'
GO
select * from tbselect * from tb a where (select count(*) from tb where 姓名=a.姓名)>1