admin管理员组文章数量:1025502
I need to make a select from MySQL with the selection values in an array. I have data like this:
const CompaniesRelation = [{CompanyId: ""},{CompanyId: ""},{CompanyId: ""},{CompanyId: ""},{CompanyId: ""}];
With Companies Relation I need to select/find, I need to get all the information for each CompanyId's:
const Companies: Array<Company> = await getRepository(Company).find({ where:{ CompanyId: CompaniesRelation[0].CompanyId, IsActive: true} });
I'm using TypeOrm in Angular. I need to select the information for each object into CompaniesRelation. I need to use fewest selects to DB, in other words, foreach is not the way.
For the final result I need to have an array with all the information of each pany in Companies Relation, like this:
[{CompanyId: "", Name: "", fk:""},{CompanyId: "", Name: "", fk:""},{CompanyId: "", Name: "", fk:""}]
I need to make a select from MySQL with the selection values in an array. I have data like this:
const CompaniesRelation = [{CompanyId: ""},{CompanyId: ""},{CompanyId: ""},{CompanyId: ""},{CompanyId: ""}];
With Companies Relation I need to select/find, I need to get all the information for each CompanyId's:
const Companies: Array<Company> = await getRepository(Company).find({ where:{ CompanyId: CompaniesRelation[0].CompanyId, IsActive: true} });
I'm using TypeOrm in Angular. I need to select the information for each object into CompaniesRelation. I need to use fewest selects to DB, in other words, foreach is not the way.
For the final result I need to have an array with all the information of each pany in Companies Relation, like this:
[{CompanyId: "", Name: "", fk:""},{CompanyId: "", Name: "", fk:""},{CompanyId: "", Name: "", fk:""}]
Share
Improve this question
edited Mar 2, 2021 at 23:01
Edward
8,6462 gold badges40 silver badges47 bronze badges
asked Mar 1, 2021 at 20:42
user14860979user14860979
1271 gold badge3 silver badges14 bronze badges
2
- 1 Typeorm also has the IN operator. – Jonas Wilms Commented Mar 1, 2021 at 20:45
- @JonasWilms Do you have an example or link of sintaxys? – user14860979 Commented Mar 1, 2021 at 21:47
1 Answer
Reset to default 5How you use WHERE x IN explained in find options for find*, and adding WHERE expression for QueryBuilder.
First get your Company Ids into an array of integers:
const panyIds = [1, 2, 3];
or
const panySel = [{ CompanyId: 1 }, { CompanyId: 2 }, { CompanyId: 3 }];
const panyIds = panySel.map(a => a.CompanyId);
Then you can use the In operator with find
import {In} from "typeorm";
const panyList = await getRepository(Company)
.find({ where: { CompanyId: In (panyIds ) } });
Or you can use the TypeOrm QueryBuilder with "IN" (note the :...
syntax)
const panyList = await getRepository(Company)
.createQueryBuilder()
.where("CompanyId IN (:...ids )", { ids: panyIds ) })
.getMany();
I need to make a select from MySQL with the selection values in an array. I have data like this:
const CompaniesRelation = [{CompanyId: ""},{CompanyId: ""},{CompanyId: ""},{CompanyId: ""},{CompanyId: ""}];
With Companies Relation I need to select/find, I need to get all the information for each CompanyId's:
const Companies: Array<Company> = await getRepository(Company).find({ where:{ CompanyId: CompaniesRelation[0].CompanyId, IsActive: true} });
I'm using TypeOrm in Angular. I need to select the information for each object into CompaniesRelation. I need to use fewest selects to DB, in other words, foreach is not the way.
For the final result I need to have an array with all the information of each pany in Companies Relation, like this:
[{CompanyId: "", Name: "", fk:""},{CompanyId: "", Name: "", fk:""},{CompanyId: "", Name: "", fk:""}]
I need to make a select from MySQL with the selection values in an array. I have data like this:
const CompaniesRelation = [{CompanyId: ""},{CompanyId: ""},{CompanyId: ""},{CompanyId: ""},{CompanyId: ""}];
With Companies Relation I need to select/find, I need to get all the information for each CompanyId's:
const Companies: Array<Company> = await getRepository(Company).find({ where:{ CompanyId: CompaniesRelation[0].CompanyId, IsActive: true} });
I'm using TypeOrm in Angular. I need to select the information for each object into CompaniesRelation. I need to use fewest selects to DB, in other words, foreach is not the way.
For the final result I need to have an array with all the information of each pany in Companies Relation, like this:
[{CompanyId: "", Name: "", fk:""},{CompanyId: "", Name: "", fk:""},{CompanyId: "", Name: "", fk:""}]
Share
Improve this question
edited Mar 2, 2021 at 23:01
Edward
8,6462 gold badges40 silver badges47 bronze badges
asked Mar 1, 2021 at 20:42
user14860979user14860979
1271 gold badge3 silver badges14 bronze badges
2
- 1 Typeorm also has the IN operator. – Jonas Wilms Commented Mar 1, 2021 at 20:45
- @JonasWilms Do you have an example or link of sintaxys? – user14860979 Commented Mar 1, 2021 at 21:47
1 Answer
Reset to default 5How you use WHERE x IN explained in find options for find*, and adding WHERE expression for QueryBuilder.
First get your Company Ids into an array of integers:
const panyIds = [1, 2, 3];
or
const panySel = [{ CompanyId: 1 }, { CompanyId: 2 }, { CompanyId: 3 }];
const panyIds = panySel.map(a => a.CompanyId);
Then you can use the In operator with find
import {In} from "typeorm";
const panyList = await getRepository(Company)
.find({ where: { CompanyId: In (panyIds ) } });
Or you can use the TypeOrm QueryBuilder with "IN" (note the :...
syntax)
const panyList = await getRepository(Company)
.createQueryBuilder()
.where("CompanyId IN (:...ids )", { ids: panyIds ) })
.getMany();
本文标签: javascriptSelect from MySQL WHERE IN values arrayStack Overflow
版权声明:本文标题:javascript - Select from MySQL WHERE IN [values array] - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745634328a2160351.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论