Harry
Harry

Reputation: 522

bookshelf.js - nested statement in where condition

How can I write the below query in bookshelf.js?

select * from Table where (b=2 or b=3) and c = 4;

I am mainly concerned about the nested part (b=2 or b=3).

Upvotes: 0

Views: 1651

Answers (2)

Badal Khatri
Badal Khatri

Reputation: 1

BookShelfTabelModel.where((qb) => {
        qb.where((qb1) => {
          qb1
            .where({
              b: 2,
            })
            .orWhere({
              b: 3,
            });strong text
        });
        qb.andWhere({ c: 4 });
      }).fetchAll()

Note

  • This will generate query like following
    select * from table where ((b = 2 or (b = 3)) and c = 4)

Upvotes: 0

Harry
Harry

Reputation: 522

Since bookshelf is built on knex, I was searching for this in knex.js documentation.

Reference Link: https://knexjs.org/#Builder-where

knex('Table').where(function(){
  this.where('b', 2).orWhere('b', 3)
}).andWhere({'c': 4})

Upvotes: 3

Related Questions