sql把查询结果添加到已存在的表中
答案:5 悬赏:60 手机版
解决时间 2021-04-03 23:30
- 提问者网友:藍了天白赴美
- 2021-04-03 07:35
sql把查询结果添加到已存在的表中
最佳答案
- 五星知识达人网友:玩家
- 2021-04-03 08:08
你的意思是把某一shopid的goods数据换一个shopid插入到goods表中,是这个意思吗?追问是的,shopid的值取至shop表中的id字段追答insert into goods
select @id, name,type...
from goods where shopid = @shopid
@id是shop中你想要替换的id
@shopid是goods中已有数据的shopid
不知道这样可否追问@id要取至shop表中的id,批量插入追答你的取id条件是神马追问SHOP表中的所有id。追答你用游标吧。。。追问不会,能加下Q问你么?追答你百度下游标就知道了
Declare CurName Cursor For
select distinct ID from shop
Open CurName
declare @ID varchar()
Fetch next from CurName into @ID
while @@fetch_status=0
begin
insert into goods
select @ID, name..
from goods where shoid=@shopid
fetch next from CurName into @ID
end
close CurName
deallocate CurName
跟之前一样@shopid换成已有数据的shopid
游标名CurName随便取追问放到查询语句里,运行有错..追答select Name...
INTO #temp
from goods where shopid=@shopid
Declare CurName Cursor For
select distinct ID from shop
Open CurName
Declare @ID INT
Fetch next from CurName into @ID
while @@fetch_status=0
begin
insert into goods (Shopid, Name...)
select @ID, Name...
from #temp
fetch next from CurName into @ID
END
close CurName
deallocate CurName
DROP TABLE #temp
GO
--修改后测试了下,没问题了。。。
select @id, name,type...
from goods where shopid = @shopid
@id是shop中你想要替换的id
@shopid是goods中已有数据的shopid
不知道这样可否追问@id要取至shop表中的id,批量插入追答你的取id条件是神马追问SHOP表中的所有id。追答你用游标吧。。。追问不会,能加下Q问你么?追答你百度下游标就知道了
Declare CurName Cursor For
select distinct ID from shop
Open CurName
declare @ID varchar()
Fetch next from CurName into @ID
while @@fetch_status=0
begin
insert into goods
select @ID, name..
from goods where shoid=@shopid
fetch next from CurName into @ID
end
close CurName
deallocate CurName
跟之前一样@shopid换成已有数据的shopid
游标名CurName随便取追问放到查询语句里,运行有错..追答select Name...
INTO #temp
from goods where shopid=@shopid
Declare CurName Cursor For
select distinct ID from shop
Open CurName
Declare @ID INT
Fetch next from CurName into @ID
while @@fetch_status=0
begin
insert into goods (Shopid, Name...)
select @ID, Name...
from #temp
fetch next from CurName into @ID
END
close CurName
deallocate CurName
DROP TABLE #temp
GO
--修改后测试了下,没问题了。。。
全部回答
- 1楼网友:千夜
- 2021-04-03 10:24
insert [已存在表名] (select * from [表名])
insert [已存在表名] 查询语句
insert [已存在表名] 查询语句
- 2楼网友:话散在刀尖上
- 2021-04-03 09:47
select * from goods g,shop s where s.id=g.shopid and g.shopid=x; x为你想要查询的那一行。不知道是不是你想要的效果,欢迎追问。
- 3楼网友:归鹤鸣
- 2021-04-03 09:20
你的表goods里面有相同的数据列吗?
如果有相同的 insert into goods (数据列1,数据列2、、、) values (select from shop where goods.shopid=shop.id)
如果有相同的 insert into goods (数据列1,数据列2、、、) values (select from shop where goods.shopid=shop.id)
- 4楼网友:舊物识亽
- 2021-04-03 09:14
如果可以的话 麻烦把两张表的关系描述的再清楚一点
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯