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();
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745634329a4637296.html
评论列表(0条)