最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Select from MySQL WHERE IN [values array] - Stack Overflow

matteradmin6PV0评论

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
Add a ment  | 

1 Answer 1

Reset to default 5

How 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();
Post a comment

comment list (0)

  1. No comments so far