【发布时间】:2021-06-28 10:00:51
【问题描述】:
我想选择包含我正在使用 typeorm ( SQL ) 的特定类别的文章 假设我有
articleTable: [
{
id: 1,
title: 'string'
},
{
id: 2,
title: 'string'
}
];
categoryTable: [
{
id: 1,
title: 'string'
}, {
id: 2,
title: 'string'
}
]
article_categories: [
{
categoryId: 1,
articleId: 2
},{
categoryId: 1,
articleId: 1
},{
categoryId: 2,
articleId: 2
}
]
所以问题是我想选择所有具有确切类别 = [1,2] 等的文章 这意味着如果一篇文章只有类别 1,我不想选择它。
使用此查询,我得到了结果,但它的 id 不完全匹配,而是返回分别具有 id 1 和 2 的文章
async filterData(filterArticleDto: FilterArticlesDto, categoryIds: number[]) {
const qb = this.createQueryBuilder('article');
qb.distinct(true);
if (categoryIds.length) {
qb.innerJoin(
'article.categories',
'categories',
`categories.category IN (:...ids)`,
{
ids: categoryIds,
},
);
qb.leftJoinAndSelect('categories.category', 'catsData');
}
qb.take(filterArticleDto.limit);
return await qb.getMany();
}
【问题讨论】:
标签: sql node.js postgresql typeorm