admin管理员组文章数量:1289867
I want to reproduce a query where the values are the result of a select in typeorm.
The query i want to reproduce is the one i provide here, but i can't find anything in typeorm documentation.
(Isnt important what the query does for the answer, i only need to know how to write that "SELECT
" in typeorm)
/classes/_query_builder_insertquerybuilder_.insertquerybuilder.html#values
INSERT INTO `furgpezzo`(`giacenza`, `giacenzaMin`, `pezzoBarcode`, `furgoneTarga`, `invStandardId`)
select '0', '5', '234234234234', f.`furgoneTarga`, '1'
from `furgpezzo` f
where f.`invStandardId` = '1'
group by f.`furgoneTarga`
something like:
(Edit:)
return await this.dmDatabase.getRepository(FurgPezzo)
.createQueryBuilder()
.insert()
.into(FurgPezzo)
.values( //here put my select )
I want to reproduce a query where the values are the result of a select in typeorm.
The query i want to reproduce is the one i provide here, but i can't find anything in typeorm documentation.
(Isnt important what the query does for the answer, i only need to know how to write that "SELECT
" in typeorm)
http://typeorm.delightful.studio/classes/_query_builder_insertquerybuilder_.insertquerybuilder.html#values
INSERT INTO `furgpezzo`(`giacenza`, `giacenzaMin`, `pezzoBarcode`, `furgoneTarga`, `invStandardId`)
select '0', '5', '234234234234', f.`furgoneTarga`, '1'
from `furgpezzo` f
where f.`invStandardId` = '1'
group by f.`furgoneTarga`
something like:
(Edit:)
return await this.dmDatabase.getRepository(FurgPezzo)
.createQueryBuilder()
.insert()
.into(FurgPezzo)
.values( //here put my select )
Share
Improve this question
edited Oct 23, 2019 at 14:51
doub1ejack
11.2k20 gold badges77 silver badges130 bronze badges
asked Aug 5, 2019 at 17:11
marco-zan reymerkmarco-zan reymerk
691 gold badge1 silver badge6 bronze badges
2 Answers
Reset to default 10This is the neatest solution I could e up with, hope it helps future explorers. The answer ments inspired me.
const [selectQuery, params] = this.entityManager
.createQueryBuilder()
.select("user.name")
.from(User, "user")
.where("user.id IN (:...ids)", {
ids: [
"87654321-1234-1234-1234-123456789abc",
"a9876521-aabb-ccdd-ffaa-123abcdefccc",
],
})
.getQueryAndParameters();
await this.entityManager.query(
`
INSERT INTO dummy("name")
${selectQuery}
`,
params
);
Yes you can. From docs:
You can easily create subqueries. Subqueries are supported in
FROM
,WHERE
andJOIN
expressions.
Refer to the following posts for example: TypeORM subqueries, Typeorm subquery add select.
You can use subqueries:
return await this.dmDatabase.getRepository(InvStandard)
.insert()
.values(qb => {qb.select(FurgPezzo).where()})//here put my select
// with subquery->
return await this.dmDatabase.getRepository(InvStandard)
.insert()
.values(qb => {qb.select(FurgPezzo).where(
const subQuery = qb.subQuery()
// your subquery builder
return "your condition " + subQuery;)})
本文标签: javascriptInsert using a select in typeormStack Overflow
版权声明:本文标题:javascript - Insert using a select in typeorm - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741487935a2381490.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论