求循环查询赋值SQL语句!在线等~!

来源:百度知道 编辑:UC知道 时间:2024/06/30 10:55:16
两个表:把查询select tag as tags from dedejcw_taglist where aid='11350'
返回多个值,同一aid多个不同tags值(比如:返回三个值:都是同一aid,不同tags值),插入到另一表对应的aid字段中的字段kewords中,唯一值。
顺便写个循环语句,一次性执行完,谢谢~!
语句是要在mysql中执行的。
问题描述:
两个表:第一个表:查询字段tag的值:表的设置为:aid有相同(aid为另一表主键)对应的tag值不同
第二个表:主键id即表一的aid,字段keword
要求:根据表二的aid主键唯一性,把查询表一的tag值插入到表二的kewoed字段。

第二个表:主键id即表一的aid
麻烦看明白点~!

再描述:
select tag as tags from dedejcw_taglist where aid='11350'
这个aid就是表二的主键,有很多数据
要求根据这个查询结果(查询表一的结果为多条记录)一一插入到表二字段keyword中
两个表的关系就是aid=id关联在。

回复 rjiang2008 的回答
表一的查询是你那样的
要求是把这个查询结果全部插入到表二里,用“,”分割,表一的aid即是表二的id,id唯一性,根据这个插入
写循环查询语句

两个表
表1
aid tag
1 文字1
1 文字2
1 文字3
2 文字5
2 文字6
2 文字7
表2 id为主键
id keywords
1
2
3
实现结果在表2里面
id keywords
1 文字1,文字2,文字3

ALTER PROCEDURE p_test AS
declare @tags varchar(10)
declare @ret varchar(50)
declare @aid int
declare cur_aid cursor for select distinct aid from dedejcw_taglist
open cur_aid
fetch next from cur_aid into @aid
while @@fetch_status = 0
begin
set @ret = ''
declare cur_tags cursor for select tag from dedejcw_taglist where aid = @aid
open cur_tags
fetch next from cur_tags into @tags
while @@fetch_status = 0
begin
set @ret = @ret +@tags + ','
fetch next from cur_tags into @tags
end
close cur_tags
deallocate cur_tags
update dedejcw_archives
set keywords = substring(@ret,1,len(@ret)-1)
where id = @aid
fetch next from cur_aid into @aid
end
close cur_aid
deallocate cur_aid

是要查询kewords这个字段吗?
select kewords
from (select tag as tags
from dedejcw_taglist
where aid='11350')a ,tab