我试图使用底层的knex.js语法将bookshelf.js中的两列相乘。
这里是我想做的纯SQL(至少select
部分):
select sum(quantity * price) as sum
from products
inner join orders_products on orders_products.product_id = products.id
inner join orders on orders_products.order_id = orders.id
where orders.id = 1
group by (orders_products.order_id)
这是我的疑问:
return new Orders().fetch({
withRelated: [{
'sum': function(qb) {
/*
how could I multiplay values here, i.e.
qb.sum('products.price * orders_products.quantity as sum');
*/
qb.sum('products.price as sum');
qb.groupBy('orders_products.order_id');
qb.limit(1);
}
}, 'products', 'owner', 'assignedUsers', 'comments.user']
}).then(function(response) {
return response.toJSON();
});
正如代码中所评论的那样,我想实际执行此操作:qb.sum('products.price * orders_products.quantity as sum');
这当然不起作用,因为我收到的错误是没有名为products.price * orders_products.quantity
的列。
有什么想法吗?