现在我有一个表 t ,表中字段 有 id(int) ,num1(varchar2) ,num2(varchar2) ,我想求的是表中有不重复的num2,和num1的总和,但现在有个难点的地方是 num1 和num2中的内容有可能相等,
t中数据 假如位 id num1 num2
1 a b
2 b d
3 c e
4 d b
5 a f
我想得到的结果应该为 (a,b,c,d,e,f) ,那这个sql语句应该怎么写呢?敬请指教!!!
------解决方案--------------------
- SQL code
--这是小梁写的,其他方法参照你我sql server版的贴.CREATE TABLE t(id int,num1 varchar(1),num2 varchar(1))insert into t select 1,'a','b' UNION ALLselect 2,'b','d' UNION ALLselect 3,'c','e' UNION ALLselect 4,'d','b' UNION ALLselect 5,'a','f'DECLARE @resualt VARCHAR(200)SET @resualt='('SELECT @resualt=@resualt+','+num1 FROM(SELECT distinct num1 FROM tUNIONSELECT distinct num2 FROM t) TtSELECT REPLACE(@resualt,'(,','(')+')'drop table t/*(所影响的行数为 5 行) ---------------------------------------------------------------------------------------------------------------- (a,b,c,d,e,f)(所影响的行数为 1 行)*/