如何与Bookshelfjs查询构建器结合使用?

问题描述 投票:0回答:1

我在弄清楚如何添加其中包含“或”查询的多个“和”查询时遇到问题

我最终想要的结果是这样的:

select * from model where attribute1 = value1 and (attribute2 = value2 or attribute3 = value2) 

这是我目前正在尝试的代码:

let result = await new Model().query(qb => {
   qb.where('attribute1', 'value1')

   qb.where(function () {
      this.where('attribute2', 'value2')
      this.orWhere('attribute3', 'value2')

   })
}).fetchAll()

但是问题是查询只执行我写的第一个条件

如果我首先将括号“或”作为条件,则输出为:

select * from model where (attribute2 = value2 or attribute3 = value2)

虽然我把正常情况放在首位,但输出是:

select * from model where attribute1 = value1

查询似乎未使用“和”语句“附加”(如果这是正确的词)

我知道,如果我使用普通的where语句,查询就会“追加”

示例:

let result = await new Model().query(qb => {
   qb.where('attribute1', 'value1')
   qb.where('attribute2', 'value2')
}).fetchAll()

输出:

select * from model where attribute1 = value1 and attribute2 = value2

有什么建议吗?

knex.js bookshelf.js
1个回答
0
投票

好像我在使用qb.debug(true)时看错了查询,而且非常相似。

上面的代码按预期工作。不便之处,敬请原谅。

© www.soinside.com 2019 - 2024. All rights reserved.