求一簡單SQL語句

來源:互聯網
上載者:User
求一簡單SQL語句 Delphi / Windows SDK/API
http://www.delphi2007.net/DelphiDB/html/delphi_20061216115320272.html
有表A  
   
  A         B           C  
   
  中       1        
  國       2  
  首       3  
  都       4  
  北       5  
  京       6  
   
   
  求一sql語句實現  
  把所有記錄的一個字串欄位自動連接  
  如上表用一條查詢語句最終實現‘中國首都北京’  
  多謝了

設表名為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  
  但這種做法遠遠沒有用程式實現簡單高效  
 

程式的話:  
  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;

同意樓上的,我再給個預存程序:  
   
   
  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  
  end  
   
  select   @str

用SQL真實不好實現  
  但我的表欄位也沒有規律,B列是不確定的字元,不一定是1,2,3...,並且記錄數還不是確定的  
  A         B           C  
   
  中       qq        
  國       ee  
  首       qwr  
  都       rety  
  北       dfg  
  京       trf  
  和       kjk  
  a         sdfs  
   
  我的本意是查詢,給一個字串參數,比如‘首’,我要把這個表的A欄位組合起來,查參數‘首’是否在這個表的A欄位組合中,也就是給的參數是否在‘中國首都北京’中,要是用預存程序的話怎麼寫?效率比程式高嗎?  
   
 

你查參數是否在A的欄位組合中與查詢參數是否在A上有區別嗎?  
   
  設表為temp,SELECT   A   FROM   TEMP   WHERE   :P=A,這樣難道查不出參數是否在A列上?

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.