Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nested AND and OR conditions on find methods (not queryBuilder)

Precondition: I'm using Typeorm to perform database queries and I can't use the queryBuilder methods to create this query, but only the find method.

I need to translate a query like:

SELECT * FROM address where zip = 123 and (street = 'asd' or city = 'New York');

I know that the solution can be trivial using the usual queryBuilder, but in this particular case I cannot use it.

Is it possible to implement these nested and/or conditions only using the find method?

like image 789
LucaRoverelli Avatar asked Dec 17 '25 11:12

LucaRoverelli


1 Answers

Can you please try this?

Address.find([{
  zip: 123, street: 'asd'
}, {
  zip: 123, city: 'New York'
}]);

if you have just and, or conditions, yes! it's possible to implement the query only with find method. In typeorm query, you can represent or with the array of the conditions.

zip = 123 and (street = 'asd' or city = 'New York')

is same as

(zip = 123 and street = 'asd') or (zip = 123 and city = 'New York')
like image 78
topmoon Avatar answered Dec 20 '25 03:12

topmoon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!