Hi guys, im looking for help in creating complex query for me.
So i have two tables
Product:
- id
... - categories (joined with Category table)
Discount: - id
... - productId (as fk)
- categoryId (as fk also)
and there is a product (id: 'someproductid', categories: ['somecategory']),
an category (id: 'somecategory')
and a discount (id: 'somediscountid', productId: null, categoryId: 'somecategory')
What I'm trying to do is to provide only product.id and find those discounts which has product's category. Query starts in discount service and it's look like that for now (i'll skip some code blocks ofc)
let query = this.discountRepository.createQueryBuilder('discount');
query
.leftJoinAndSelect('discount.product', 'product')
.leftJoinAndSelect(
'product.categories',
'productCategories',
'productCategories.audit_rd IS NULL AND productCategories.audit_ru IS NULL',
)
.leftJoinAndSelect('discount.users', 'users')
.leftJoinAndSelect('discount.category', 'category');
query.select([
'discount',
'product.id',
'product.name',
'productCategories.name',
]);
if (dto.productId) {
query.andWhere(
new Brackets(qb => {
qb
.orWhere('product.id = :productId', { productId: dto.productId})
.orWhere('category.name = productCategories.name)
// Also tried with `IN productCategories.name` but it throws error
})
)
}
Plz help