有表AA    B     C中   1   
国   2
首   3
都   4
北   5
京   6
求一sql语句实现
把所有记录的一个字符串字段自动连接
如上表用一条查询语句最终实现‘中国首都北京’
多谢了

解决方案 »

  1.   

    设表名为T1(表名为A会与别名冲突),以下SQL可得到你要的
    select a.a+b.a+c.a+d.a+e.a+f.a as aa from t1 a,t1 b,t1 c,t1 d,t1 e,t1 f where a.b=b.b-1 and b.b=c.b-1 and c.b=d.b-1 and d.b=e.b-1 and e.b=f.b-1
    但这种做法远远没有用程序实现简单高效
      

  2.   

    程序的话:
    procedure TForm1.Button1Click(Sender: TObject);
    var
      s:string;
    begin
      s:='';
     with ADOQuery1 do
     begin
        sql.Text:='select a from t1 order by b';
        open;
        while not eof do
        begin
          s:=s+FieldByName('a').AsString;
          next;
        end;
     end;
     ShowMessage(s);
    end;
      

  3.   

    同意楼上的,我再给个存储过程:
    select @count=count(*) from T1
    declare @i int
    set @i=1
    declare @str varchar(8000)
    set @str=''
    while @i<=@count
    begin 
      select @str=@str+B from T1 where c=@i
      set @i=@i+1
    endselect @str
      

  4.   

    用SQL真实不好实现
    但我的表字段也没有规律,B列是不确定的字符,不一定是1,2,3...,并且记录数还不是确定的
    A    B     C中   qq   
    国   ee
    首   qwr
    都   rety
    北   dfg
    京   trf
    和   kjk
    a    sdfs我的本意是查询,给一个字符串参数,比如‘首’,我要把这个表的A字段组合起来,查参数‘首’是否在这个表的A字段组合中,也就是给的参数是否在‘中国首都北京’中,要是用存储过程的话怎么写?效率比程序高吗?
      

  5.   

    你查参数是否在A的字段组合中与查询参数是否在A上有区别吗?设表为temp,SELECT A FROM TEMP WHERE :P=A,这样难道查不出参数是否在A列上?