1

I want to do multiple where query without effect data. I want to get data that include at least 1 data per array. Pseudo code

data =[1,3]
array1 = [1,2]
array2 = [3,4]

if(data.IsIntersect(array1) and data.IsIntersect(array2))

IsIntersect checks are there a intersection beetween arrays


I did so far

queryBuilder.andWhere(
      'properties.id IN (:...sizeIds) AND properties.id IN (:...colorIds)',
      { sizeIds: [1, 2], colorIds: [3, 4] },
);

It returns empty because firstly checks properties for 'sizeIds' then it checks for 'colorIds'. For example

properties includes 1,3
check for sizeIds, returns 1
check for colorIds, return empty

How can I do that with typeORM?

I.Bozcan
  • 45
  • 1
  • 7

1 Answers1

0

How can properties.id be 1 and 3? And if it is, how could 1 or 3 be in both? You're asking for the impossible.

I assume you mean to ask for when properties.id is 1 or 3, because if it is [1,3] then you should use the postgres array syntax {1,3} & the ANY keyword (some variation on this: Check if value exists in Postgres array).

tldr, I think all you need is brackets and OR instead of AND:

queryBuilder.andWhere(
      '(properties.id IN (:...sizeIds) OR properties.id IN (:...colorIds))',
      { sizeIds: [1, 2], colorIds: [3, 4] },
);

If properties.id is in fact an array, then please add the entity definition to your question. If you want to merge the rows where properties.id is in the list you will need a GROUP BY (https://orkhan.gitbook.io/typeorm/docs/select-query-builder).

ᴓᴓᴓ
  • 1,178
  • 1
  • 7
  • 18
  • Thanks for your answer. There are properties of product and they are right joined. Basically logic should work as ANY keyword but I dont have array of properties. I want to get products if properties is in array parameters. For example, properties of product 1 and 3. Array parameters, [1,2] and [3,4]. So, product pass the condition. Other product is 2,7. It doesnt pass. – I.Bozcan Jan 22 '22 at 08:30
  • "For example, properties of product 1 and 3. Array parameters, [1,2] and [3,4]. So, product pass the condition. Other product is 2,7. It doesnt pass" Then just change the OR to AND? – ᴓᴓᴓ Jan 23 '22 at 21:27