gpt4 book ai didi

javascript - Bookshelf.js 或 Knex.js 中 2 个表的外部连接

转载 作者:数据小太阳 更新时间:2023-10-29 06:06:54 26 4
gpt4 key购买 nike

我是 Bookshelf.js 和 knex 的新手。我需要在 Bookshelf/knex 中编写一个与此等效的查询

SELECT Inv.*, Comp.* 
FROM Inv, Comp
WHERE Inv.uId =2 AND Comp.cId = Inv.cId;

Inv 表有:

Id     |  primary key, integer  not nullcol1   | string datacId    | integer, foreign key references C tableuId    | integer  foreign key reference U table      

比较表有:

cId     |  primary key, integer  not null col3    | string data

最佳答案

使用 knex 查询生成器。假设 Invs 是模型 Inv 的集合,它可能类似于:

Invs.forge().query(function(qb) {
qb.join('Comp', 'Comp.cId', '=', 'Inv.cId');
qb.where('Inv.uId', 2);
}).fetch({withRelated: 'comps'}).then(...)

其中 comps 是 Inv 模型中定义的关系。您也可以完全跳过 bookshelf 并直接通过 bookshelf.knex 使用 knex 来形成您需要的确切查询:

bookshelf.knex('Inv')
.join('Comp', 'Comp.cId', '=', 'Inv.cId')
.where('Inv.id', 2)
.select()
.then(...)

关于javascript - Bookshelf.js 或 Knex.js 中 2 个表的外部连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20847367/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com