CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
英特尔®游戏设计大赛100美元现金周周送 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  MS-SQL Server >  疑难问题

求查询语句,两个表A,B,一个表的数据作为另一个表的字段。急急!!!

楼主shot_big(smile)2003-11-04 15:57:33 在 MS-SQL Server / 疑难问题 提问

求查询语句,两个表A,B  
  A有字段id,name,sex,date  
  B有字段id,aid,score,stu,www  
  其中A.id=B.aid,在B中可有多条数据对应A表当中。现要求写一查询语句合并二表  
  对应A表当中B的多条数据,合并后作为一个字段插入到A当中。  
  例如:  
   
  A:1,小王,男,2003-10-4  
  B:1,1,54,999,无  
        2,1,87,222,有  
         
  查询的结果为:1,小王,男,2003-12-4,[54:999:无;87:222:有] 问题点数:20、回复次数:15Top

1 楼sdhdy(大江东去...)回复于 2003-11-04 16:15:17 得分 5

--可以用函数来解决  
  create   function   test(@id   int)  
  returns   varchar(8000)  
  as  
  begin  
  declare   @sql   varchar(8000)  
  set   @sql=''  
  select   @sql=@sql+cast(score   as   varchar)+':'+cast(stu   as   varchar)+':'+date+';'   from   b   where   aid=@id  
  set   @sql=left(@sql,len(@sql)-1)  
  set   @sql='['+@sql+']'  
  return(@sql)  
  end  
   
  --执行  
  select   id,name,sex,date,dbo.test(id)   as   asdf   from   aTop

2 楼wzh1215(懒猫)回复于 2003-11-04 16:19:16 得分 2

declare   @Sql   varchar(8000)  
  set   @Sql='',  
  select   @Sql=@Sql+';'+convert(varhcar(10),score)+':'+convert(varhcar(10),stu)+':'+convert(varchar(10),www))   from   B   where   aid=1  
  set   @Sql=right(@Sql,len(@Sql)-1)  
  select   id,name,sex,date,@Sql   as   ss   from   A   where   id=1Top

3 楼sdhdy(大江东去...)回复于 2003-11-04 16:19:19 得分 3

--modify  
  create   function   test(@id   int)  
  returns   varchar(8000)  
  as  
  begin  
  declare   @sql   varchar(8000)  
  set   @sql=''  
  select   @sql=@sql+cast(score   as   varchar)+':'+cast(stu   as   varchar)+':'+www+';'   from   b   where   aid=@id  
  set   @sql=left(@sql,len(@sql)-1)  
  set   @sql='['+@sql+']'  
  return(@sql)  
  end  
   
  --执行  
  select   id,name,sex,date,dbo.test(id)   as   asdf   from   a  
  Top

4 楼aierong()回复于 2003-11-04 16:47:53 得分 2

 
   
  create   table   aa(id   int,name   varchar(100),sex   varchar(100),date   datetime)  
  create   table   bb(id   int,aid   int,score   int,stu   int,www   varchar(100))  
   
  insert   into   aa   select   1,'小王','男','2003-10-4'  
  insert   into   bb   select   1,1,54,999,'无'  
  insert   into   bb   select   2,1,87,222,'有'  
   
  create   function   fun(@aid   int)  
  returns   varchar(4000)   as  
  begin  
  declare   @str   varchar(8000)  
  select   @str=''  
  select   @str=@str+convert(varchar(4000),score)+':'+convert(varchar(4000),stu)+':'+www+':'  
  from   bb   where   aid=@aid  
  select   @str=left(@str,len(@str)-1)  
  return   @str  
  end  
   
  select   a.*,b.jia    
  from   aa   a   join    
  (select   max(id)   id,aid,jia  
  from   (  
  select   *,dbo.fun(aid)   jia   from   bb)   temb  
  group   by   aid,jia)   b  
  on   a.id=b.aid  
  Top

5 楼shot_big(smile)回复于 2003-11-04 16:51:27 得分 0

to     sdhdy(大江东去...):有错误啊  
   
  Server:   Msg   170,   Level   15,   State   1,   Line   1  
  Line   1:   Incorrect   syntax   near   'function'.  
  Server:   Msg   137,   Level   15,   State   1,   Line   7  
  Must   declare   the   variable   '@id'.  
  Server:   Msg   178,   Level   15,   State   1,   Line   8  
  A   RETURN   statement   with   a   return   status   can   only   be   used   in   a   stored   procedure.Top

6 楼shot_big(smile)回复于 2003-11-04 17:59:06 得分 0

up  
  Top

7 楼youngby(C-love)回复于 2003-11-04 18:23:41 得分 1

错误:  
  检查你的语句  
  第一行是否有(@id   int)  
   
  Top

8 楼youngby(C-love)回复于 2003-11-04 18:44:16 得分 2

create   procedure   test  
  as  
  declare   @sql   varchar(8000)  
  declare   @Count   int  
  declare   @myTd   int  
  set   @myId=1  
  set   @sql=''  
  select    
  IDENTITY(int,1,1)   as   Id,    
  @sql=@sql+cast(score   as   varchar)+':'+cast(stu   as   varchar)+':'+www+';'   as   mysql   to   #myTable   from   b   inner   join   a     on   a.id=b.aid  
   
  select   @count=Max(Id)   from   #myTempTable  
  select   @mySql   varchar(60)  
  WHILE   @count   <=   @myID  
  BEGIN  
  select   @mySql=mySql   from   #myTable   where   ID=@myID  
  set   @mySql=left(@sql,len(@sql)-1)  
  set   @mySql='['+@sql+']'  
  insert   into   a   ("新列的列名")  
  values(@mySql)  
  @myId=@myID   +1  
  Go  
   
  Top

9 楼youngby(C-love)回复于 2003-11-04 18:49:29 得分 2

晕。忘了end   了。  
   
  如果你原来的表没有保存   [54:999:无;87:222:有]   的列。  
   
  则可以改为下面语句。  
  create   procedure   test  
  as  
  declare   @sql   varchar(8000)  
  declare   @Count   int  
  declare   @myTd   int  
  set   @myId=1  
  set   @sql=''  
  select    
  IDENTITY(int,1,1)   as   Id,    
  @sql=@sql+cast(score   as   varchar)+':'+cast(stu   as   varchar)+':'+www+';'   as   mysql   to   #myTable   from   b   inner   join   a     on   a.id=b.aid  
   
  select   @count=Max(Id)   from   #myTempTable  
  alter   table   a  
  add   新列的列名   varchar(60)   NULL  
  select   @mySql   varchar(60)  
  WHILE   @count   <=   @myID  
  BEGIN  
  select   @mySql=mySql   from   #myTable   where   ID=@myID  
  set   @mySql=left(@sql,len(@sql)-1)  
  set   @mySql='['+@sql+']'  
  insert   into   a   ("新列的列名")  
  values(@mySql)  
  @myId=@myID   +1  
  end    
  Go  
   
   
   
   
  Top

10 楼missroad2001(侠客)回复于 2003-11-04 18:53:23 得分 1

我已检查过了,完全正确。好!Top

11 楼youngby(C-love)回复于 2003-11-04 19:09:57 得分 1

昏,,事老是丢三掉四。  
  更正::  
  /**************************已经调试通过************************/  
  create   procedure   test  
  as  
  declare   @sql   varchar(8000)  
  declare   @Count   int  
  set   @sql=''  
  select    
  IDENTITY(int,1,1)   as   Id,    
  cast(score   as   varchar)+':'+cast(stu   as   varchar)+':'+www+';'   as   [mysql]   into   #myTable   from   b   inner   join   a     on   a.id=b.aid  
   
  select   @count=Max(Id)   from   #myTempTable  
  alter   table   a                                   -------添加新的列  
  add   新列的列名   varchar(60)   NULL  
  declare   @mySql   varchar(60)  
  declare   @myId   int  
  set   @myId=1  
  WHILE   @count   <=@myId    
  BEGIN  
  select   @mySql=mySql   from   #myTable   where   ID=@myId    
  set   @mySql=left(@mySql,len(@sql)-1)  
  set   @mySql='['+@mySql+']'  
  INSERT   INTO   a   (添加新的列)    
        VALUES   (@mySql)  
   
  set   @myId=@myID   +1  
  end    
  drop   table   #myTable   --删除临时表。  
  Go  
  Top

12 楼youngby(C-love)回复于 2003-11-04 19:11:37 得分 0

select   @count=Max(Id)   from   #myTempTable   =====》》  
  select   @count=Max(Id)   from   #myTable  
   
  Top

13 楼shot_big(smile)回复于 2003-11-05 09:45:50 得分 0

不能更改原来表的列,只是要求查询出来那样Top

14 楼tsqgw2001(程式猎人)回复于 2003-11-05 10:45:31 得分 1

update   a      
  set   www=(select   aid   from   b   join   a     on   A.id=B.aid)  
  where   a.id   in   (select   aid   from   b   join   a   on   a.id=b.aid)  
  Top

15 楼shot_big(smile)回复于 2003-11-07 10:54:59 得分 0

不能更改原来表的列,只是要求查询出来那样Top

相关问题

  • 求sql语句A表1字段=B表1字段+B表2字段??
  • sql语句, 从表a,表b取数据,关联字段a.hh,b.hh, 关联字段a.hh,b.hh, 取a.hh,b表c字段,select a.hh,sum(b.c) from a,b where a.hh=b.hh gr
  • 要从表A中取几个字段更新表B中的几个字段,SQL语句如何写?
  • A表的某个字段值为B表中对应记录的所有字段之和,现怎么样更新A表的该字段(sql语句)?
  • sql语句写法:将表a的某几个字段的数据按照某个字段相同的原则插入到表b的某几个字段下?
  • A表中A字段=B表中A字段+B字段的和
  • SQL语句,两个表,表A字段名类似C_1,C_2,C_3,C_4,表B(id,name)里有四条记录分别描述A表的四个字段,详细请进
  • sql语句之2问:a表插入b表记录,另外有个其他字段怎么办??????????
  • 用一条SQL语句,如何将两个表中,A表的一个字段的值复制到B表一个字段中去,
  • 查询所有符合 表a中的一个字段=表b中的一个字段的子串的SQL语句怎么写?

关键词

  • 字段
  • 语句
  • 查询
  • 数据
  • date
  • sql
  • 表
  • 查询语句
  • aid
  • varchar

得分解答快速导航

  • 帖主:shot_big
  • sdhdy
  • wzh1215
  • sdhdy
  • aierong
  • youngby
  • youngby
  • youngby
  • missroad2001
  • youngby
  • tsqgw2001

相关链接

  • SQL Server类图书

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo