日期:2014-05-17 浏览次数:20817 次
--这是小梁写的,其他方法参照你我sql server版的贴. CREATE TABLE t(id int,num1 varchar(1),num2 varchar(1)) insert into t select 1,'a','b' UNION ALL select 2,'b','d' UNION ALL select 3,'c','e' UNION ALL select 4,'d','b' UNION ALL select 5,'a','f' DECLARE @resualt VARCHAR(200) SET @resualt='(' SELECT @resualt=@resualt+','+num1 FROM ( SELECT distinct num1 FROM t UNION SELECT distinct num2 FROM t ) Tt SELECT REPLACE(@resualt,'(,','(')+')' drop table t /* (所影响的行数为 5 行) ---------------------------------------------------------------------------------------------------------------- (a,b,c,d,e,f) (所影响的行数为 1 行) */