繁体   English   中英

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

[英]Outer join of 2 tables in Bookshelf.js or Knex.js

我是Bookshelf.js和knex的新手。 我需要在Bookshelf / knex中编写与此相当的查询

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

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(...)

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM