关于insert语句中嵌套select语句
答案:2 悬赏:50
解决时间 2021-02-20 02:00
- 提问者网友:江山如画
- 2021-02-19 09:41
我从excel中读到一批数据。要用insert语句插入到A表去。。但这批数据中的某列的数据是B表的name,现在需要把表B的name对应的id插到表A。sql 应该怎么写?
最佳答案
- 二级知识专家网友:樣嘚尐年
- 2021-02-19 10:22
在VALUES子句中不能有子查询,这样就可以了:
insert into VoteRecord(IP,TopicNum) select '" + ip + "',ID from Topic where [Content]='" + topic + "'
实际生成的语句应该这样:
insert into VoteRecord(IP,TopicNum) select '192.168.1.1',ID from Topic where [Content]='123'
不过,为保证不发生错误,最好在子查询中加入TOP 1 子句或MAX()函数等,保证子查询记录是一条
insert into VoteRecord(IP,TopicNum) select '192.168.1.1',max(ID) from Topic where [Content]='123'
insert into VoteRecord(IP,TopicNum) select '" + ip + "',ID from Topic where [Content]='" + topic + "'
实际生成的语句应该这样:
insert into VoteRecord(IP,TopicNum) select '192.168.1.1',ID from Topic where [Content]='123'
不过,为保证不发生错误,最好在子查询中加入TOP 1 子句或MAX()函数等,保证子查询记录是一条
insert into VoteRecord(IP,TopicNum) select '192.168.1.1',max(ID) from Topic where [Content]='123'
全部回答
- 1楼网友:迷人小乖乖
- 2021-02-19 11:17
insert into voterecord(ip,topicnum) select '" + ip + "',id from topic where [content]='" + topic + "';
commit;
'" + ip + "'可以为常量 ,也可以是变量,常量就会在每次插入的数据中都插入相同的值
例如
insert into cd_wellbore@kftest_link(well_id,wellbore_id,wb_serial)
select well_id,well_id,’00’ from well_master 这其中的'00'就是在wb_serial中始终插入00
我要举报
如以上问答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯