如何实现insert 语句嵌套select查询

如题所述

在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'
温馨提示:答案为网友推荐,仅供参考
相似回答