首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

一个关于去除重复项的有关问题

2012-02-29 
请教大家一个关于去除重复项的问题!我在做查询时需要用到a、b、c,三个相关联表的数据。但是查询后b表的数据有

请教大家一个关于去除重复项的问题!
我在做查询时需要用到   a、b、c,三个相关联表的数据。但是查询后b表的数据有重复的   通过where   限制后还是有重复数据产生。如下
select   distinct(a.[StaffID]),b.[staffJob]   ,c.[staffPart],
convert(varchar(100),a.[EmployTime],23)   as   [EmployTime]   from   [Staffs]  
as   a,[staffJob]   as   b,[staffPart]   as   c   where   a.[StaffID]=b.[StaffID]   and   a.[staffPartID]=c.[staffPartID]
重复项为b表的数据项,请问该如何让b表没有重复数据

[解决办法]
select distinct(a.[StaffID]),b.[staffJob] ,c.[staffPart],
convert(varchar(100),a.[EmployTime],23) as [EmployTime]
from [Staffs]
as a,[staffJob] as b,[staffPart] as c
where a.[StaffID]=b.[StaffID] and a.[staffPartID]=c.[staffPartID]
and BINARY_CHECKSUM([staffJob],[staffPart]) in(
select BINARY_CHECKSUM([staffJob],[staffPart])
from staffPart
group by BINARY_CHECKSUM([staffJob],[staffPart]))
[解决办法]
select distinct(a.[StaffID]),b.[staffJob] ,c.[staffPart],
convert(varchar(100),a.[EmployTime],23) as [EmployTime]
from [Staffs]
as a,[staffJob] as b,[staffPart] as c
where a.[StaffID]=b.[StaffID] and a.[staffPartID]=c.[staffPartID]
and BINARY_CHECKSUM([staffJob],[staffPart]) in(
select BINARY_CHECKSUM([staffJob],[staffPart])
from staffPart
group by BINARY_CHECKSUM([staffJob],[staffPart])
having count(*)=1)--取没有重复的
[解决办法]
如果C表有多于一项满足a.[staffPartID]=c.[staffPartID],
那么B显示出来肯定会有重复,这也是正常合理的。

除非你愿意舍弃掉一些C表的记录
[解决办法]
select distinct(a.[StaffID]),b.[staffJob] ,c.[staffPart],
convert(varchar(100),a.[EmployTime],23) as [EmployTime] from [Staffs]
as a,(select distinct * from [staffJob]) as b,[staffPart] as c where a.[StaffID]=b.[StaffID] and a.[staffPartID]=c.[staffPartID]


这样看看?

热点排行