knex.js: orWhere 后跟多个 where 的组合

knex.js: combination of orWhere followed by multiple where

我正在使用 knex.js 编写查询,但遇到了 orWhere。

我需要这样的查询:

select 
    count(*) 
from 
    `Project` 
where 
    `Project`.`createdAt` >= '2019-11-12' 
and 
    `Project`.`createdAt` <= '2020-11-19' 
and 
    ((`Project`.`productType` = 1) 
or 
    (`Project`.`productType` = 2))

但出于某种原因,这就是我得到的结果:

select 
    count(*) 
from 
    `Project` 
where 
    `Project`.`createdAt` >= '2019-11-12' 
and 
    `Project`.`createdAt` <= '2020-11-19' 
or 
    (`Project`.`productType` = 1) 
or 
    (`Project`.`productType` = 2)

注意有两个'or,我想要and代替第一个or

这是代码:

 builder.count('*')
.from('Project')
.where('Project.createdAt', '>=', '2019-11-12')
.where('Project.createdAt', '<=', '2019-11-19')
.orWhere({'Project.productType': 1})
.orWhere({'Project.productType': 2})

非常感谢任何帮助

我知道了,你可以试试:

//
 builder.count('*')
.from('Project')
.where('Project.createdAt', '>=', '2019-11-12')
.where('Project.createdAt', '<=', '2019-11-19')
.where(function () {
    this.orWhere({'Project.productType': 1}).orWhere({'Project.productType': 2})
});

// or use arrow function
 builder.count('*')
.from('Project')
.where('Project.createdAt', '>=', '2019-11-12')
.where('Project.createdAt', '<=', '2019-11-19')
.where((bd) => {
    bd.orWhere({'Project.productType': 1}).orWhere({'Project.productType': 2})
});

希望对你有用。