knex.js / bookshelf.js - 如何多重播放列?

时间:2015-04-28 13:13:58

标签: javascript bookshelf.js knex.js

我试图使用底层的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的列。

有什么想法吗?

0 个答案:

没有答案