日期:2014-05-18  浏览次数:20472 次

字段取值问题?
eg.
id     列2       列3           列4      
1       ad         qwe2         ....
2       ef         wed           ....
3       e4g       qwe2         ....
4       sdw       ee
5       asd       qwe2
6       wed       ee


就是找出列3中的重复值,在列4中标出。

列4标示为:与id为X的有重复。

变为:
id     列2       列3           列4      
1       ad         qwe2         与id为3的有重复;与id为5的有重复
2       ef         wed          
3       e4g       qwe2         与id为1的有重复;与id为5的有重复
4       sdw       ee             与id为6的有重复
5       asd       qwe2         与id为1的有重复;与id为3的有重复
6       wed       ee             与id为4的有重复

请教高手,怎么做,主要是与id为X的有重复,如何取X值,再赋值到列4中.

先谢谢高手~~~~

------解决方案--------------------
---用函数实现吧~
CREATE FUNCTION F(@ID INT)
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str= ' '
select @str=@str+ ';与id为 '+id+ '的有重复 ' from 表名 where 列3=(select 列3 from 表名 where id <> @id)
set @str=stuff(@str,1,1, ' ')
return @str
end

select id,列2,列3,列4=dbo,f(id)
from 表名
------解决方案--------------------
--建立测试环境
create table eg(id int,列2 varchar(9),列3 varchar(9),列4 varchar(8000))
insert eg(id,列2,列3,列4)
select '1 ', 'ad ', 'qwe2 ',NULL union all
select '2 ', 'ef ', 'wed ',NULL union all
select '3 ', 'e4g ', 'qwe2 ',NULL union all
select '4 ', 'sdw ', 'ee ',NULL union all
select '5 ', 'asd ', 'qwe2 ',NULL union all
select '6 ', 'wed ', 'ee ',NULL
go
--执行测试语句
create function dbo.f_get(@列3 varchar(8000))
returns varchar(8000)
as
begin
declare @str varchar(8000)
set @str= ' '
select @str=@str+ ';与id为 '+convert(varchar(8000),id)+ '的有重复 '
from eg
where 列3=@列3
set @str=stuff(@str,1,1, ' ')
return @str
end
go
select id,列2,列3,dbo.f_get(列3)
from eg

go
--删除测试环境
drop table eg
drop function dbo.f_get
go
/*--测试结果
1 ad qwe2 与id为1的有重复;与id为3的有重复;与id为5的有重复
2 ef wed 与id为2的有重复
3 e4g qwe2 与id为1的有重复;与id为3的有重复;与id为5的有重复
4 sdw ee 与id为4的有重复;与id为6的有重复
5 asd qwe2 与id为1的有重复;与id为3的有重复;与id为5的有重复
6 wed ee 与id为4的有重复;与id为6的有重复

*/