2

typeorm 是否支持 SQL IN 子句?我正在尝试查询一个字段与多个值中的 1 个匹配的存储库。

myRepository.find({
    where: {
        SomeID: // IN [1, 2, 3, 4]
    }
});
4

3 回答 3

10

您可以为此目的使用 QueryBuilder:

const users = await userRepository.createQueryBuilder("user")
     .where("user.id IN (:...ids)", { ids: [1, 2, 3, 4] })
     .getMany();
于 2017-10-25T10:01:14.593 回答
3

我只是想建议另一种方式。

const user = await this.usersRepository
.findOne(
    {
        where: { id: In([1, 2, 3]) }
    });
于 2020-08-26T17:04:52.723 回答
1

您现在可以执行此操作(来自文档):

import {In} from "typeorm";

const loadedPosts = await connection.getRepository(Post).find({
    title: In(["About #2", "About #3"])
});

将执行以下查询:

SELECT * FROM "post" WHERE "title" IN ('About #2','About #3')
于 2019-06-30T15:28:01.773 回答