[英]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.