use*_*531 5 javascript node.js bookshelf.js knex.js
我是Bookshelf.js和knex的新手.我需要在Bookshelf/knex中编写与此相当的查询
SELECT Inv.*, Comp.*
FROM Inv, Comp
WHERE Inv.uId =2 AND Comp.cId = Inv.cId;
Run Code Online (Sandbox Code Playgroud)
Inv Table有:
Id | primary key, integer not null col1 | string data cId | integer, foreign key references C table uId | integer foreign key reference U table
Comp 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(...)
Run Code Online (Sandbox Code Playgroud)
comps是Inv模型中定义的关系.您也可以完全跳过书架,直接通过bookshelf.knex使用knex来形成您需要的确切查询:
bookshelf.knex('Inv')
.join('Comp', 'Comp.cId', '=', 'Inv.cId')
.where('Inv.id', 2)
.select()
.then(...)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5000 次 |
| 最近记录: |