Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

And and Or statements in Where Statements in Knex

Tags:

mysql

knex.js

I've been struggling with this problem for hours now... And I can not seem to figure out how to execute the following query using knex query builder...

select * from persons where first_name = "John" and (id_card_number = "1234" or id_card_number_2 = "5678")

Any help would be appreciated.

Thanks

Best Rick

like image 336
Rick McClatchie Avatar asked Oct 24 '25 16:10

Rick McClatchie


1 Answers

You can pass a function to where, which Knex will wrap all internal changes with parenthesis.

It will look like this:


knex('persons')
  .where('first_name', 'John')
  .where((whereBuilder) =>
    whereBuilder.where('id_card_number', '1234').orWhere('id_card_number_2', '5678')
  );
like image 182
felixmosh Avatar answered Oct 26 '25 07:10

felixmosh



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!