插入到表中,来自选择查询。

huangapple go评论61阅读模式
英文:

insert into table from the select query

问题

我尝试从选择查询中插入到postgresql表中,这里我有多个连接,我成功地从选择查询中获取到了数据,但当我运行insert时,出现了语法错误,错误消息是ERROR: syntax error at or near "select"

以下是我尝试的内容:

insert into pool_tags_tag ("poolId", "tagId") values (
                    select p.id as "poolId", t3.id as "tagId" from tag t3, pool p where t3.title in 
                    (select q.topic as "questionTopic" from question q 
                    join question_experience qe on qe."questionId" = q.id 
                    join "question_jobRole" qjr ON qjr."questionId" = q.id
                    join tag t on t.id = qjr."tagId"
                    join tag t2 on t2.id = qe."tagId" 
                    where t.title = 'FE' and t2.title = 'FRESHER') 
                    and p.id=144 limit 1) ON CONFLICT ("poolId", "tagId") DO nothing

如果我删除插入查询,仅运行选择查询,我可以获取到poolIdtagId

任何帮助或建议都将不胜感激。

英文:

I'm trying to insert in the postgresql table from the select query, here I have multiple joins, i successfully managed to get the data from the select query, but when i do run the insert getting the syntax error as ERROR: syntax error at or near "select"

here below is what i tried

insert into pool_tags_tag ("poolId", "tagId") values (
                    select p.id as "poolId", t3.id as "tagId" from tag t3, pool p where t3.title in 
                    (select q.topic as "questionTopic" from question q 
                    join question_experience qe on qe."questionId" = q.id 
                    join "question_jobRole" qjr ON qjr."questionId" = q.id
                    join tag t on t.id = qjr."tagId"
                    join tag t2 on t2.id = qe."tagId" 
                    where t.title = 'FE' and t2.title = 'FRESHER') 
                    and p.id=144 limit = 1) ON CONFLICT ("poolId", "tagId") DO nothing

if I remove the insert query and run only select I'm getting poolId and tagId

any helps or suggestions are really appriciated

答案1

得分: 2

You need to remove the VALUES clause because you're not actually passing some values but a SELECT statement:

insert into pool_tags_tag ("poolId", "tagId") SELECT p.id ...

英文:

You need to remove the VALUES clause because you're not actually passing some values but a SELECT statement:

insert into pool_tags_tag ("poolId", "tagId") SELECT p.id ...

huangapple
  • 本文由 发表于 2023年5月17日 16:40:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/76270109.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定