gpt4 book ai didi

sql - Knex 原始查询不工作 postgresql

转载 作者:行者123 更新时间:2023-12-05 04:14:15 24 4
gpt4 key购买 nike

我正在尝试进行以下半原始 knex 查询:

knex.raw('select sum(invoice_price * quantity) from "localhost:3000".order_item').where({cart_session: 'some_session'}).then(function(data) {
console.log(data.rows[0].sum);
});

但是,我不断收到错误消息:knex.raw(...).where is not a function

有人可以帮忙吗?

提前致谢!

最佳答案

我认为您不能以这种方式使用 raw,它通常位于您通常会使用模式构建器的某个地方。此外,看起来您也不能在 sum 子句中执行原始操作。这似乎通过在 select 中使用 raw 来做你想做的:

knex('order_item')
.select(knex.raw('sum(invoice_price * quantity)'))
.where({cart_session: 'some_session'})

这会产生以下 sql:

select sum(invoice_price * quantity) from "order_item" where "cart_session" = 'some_session'

关于sql - Knex 原始查询不工作 postgresql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35495379/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com