努力在 Node js 中用 Knex 编写一个 SELECT 查询来进行计算
Struggling to write a SELECT query with Knex in Node js that does a calculation
我正在使用 PostgresQL,我能够 运行 以下语句:
SELECT id, code, supplier, item, price, price * stockonhand AS stockvalue, stockonhand - stockprocessed AS stockleft FROM products;
这适用于 PostgresQL 和 returns 我选择的所有列以及新计算的列“stockvalue”和“stockleft”。
id | code | supplier | item | price | stockvalue | stockleft
------+-------+----------+---------+-------+------------+-----------
1002 | 15202 | EVADAM | CHZIP-X | 48.24 | 39074.4000 | 670.00
1001 | 15201 | EVADAM | ZIP-X | 42.38 | 50856.0000 | 1050.00
2 | 15204 | EVADAM | LCC-X | 33.45 | 40140.0000 | 1200.00
4 | 15203 | EVADAM | LCC-X | 33.45 | 40140.0000 | 1200.00
5 | 15205 | EVADAM | LOC-X | 36.45 | 36450.0000 | 1000.00
现在我的问题是如何转换以下代码,使其包含使用 Knex 进行的上述计算?
const handleGetProducts = (req, res, db) => {
db.select('id', 'supplier', 'code', 'item', 'description', 'price', 'stockonhand', 'stockprocessed').table('products')
.then(products => {
if (products.length) {
res.json(products)
console.log(products)
} else {
res.status(400).json('not found')
}
})
.catch(err => res.status(400).json('error getting products'))
}
module.exports = {
handleProducts: handleGetProducts
};
我到处搜索,但找不到任何对我的情况有帮助的东西。
谢谢
一种方法是使用 raw()。我相信这或多或少符合您要获取的查询:
knex.select('id', 'supplier', 'code', 'item', 'price', knex.raw('"price" * "stockonhand" as "stockvalue"'), knex.raw('"stockonhand" - "stockprocessed" as "stockleft"')).table('products')
结果如下:
select "id", "supplier", "code", "item", "price", "price" * "stockonhand" as "stockvalue", "stockonhand" - "stockprocessed" as "stockleft" from "products"
您可以在此处修改 postgres 的确切表达式:QueryLab
我正在使用 PostgresQL,我能够 运行 以下语句:
SELECT id, code, supplier, item, price, price * stockonhand AS stockvalue, stockonhand - stockprocessed AS stockleft FROM products;
这适用于 PostgresQL 和 returns 我选择的所有列以及新计算的列“stockvalue”和“stockleft”。
id | code | supplier | item | price | stockvalue | stockleft
------+-------+----------+---------+-------+------------+-----------
1002 | 15202 | EVADAM | CHZIP-X | 48.24 | 39074.4000 | 670.00
1001 | 15201 | EVADAM | ZIP-X | 42.38 | 50856.0000 | 1050.00
2 | 15204 | EVADAM | LCC-X | 33.45 | 40140.0000 | 1200.00
4 | 15203 | EVADAM | LCC-X | 33.45 | 40140.0000 | 1200.00
5 | 15205 | EVADAM | LOC-X | 36.45 | 36450.0000 | 1000.00
现在我的问题是如何转换以下代码,使其包含使用 Knex 进行的上述计算?
const handleGetProducts = (req, res, db) => {
db.select('id', 'supplier', 'code', 'item', 'description', 'price', 'stockonhand', 'stockprocessed').table('products')
.then(products => {
if (products.length) {
res.json(products)
console.log(products)
} else {
res.status(400).json('not found')
}
})
.catch(err => res.status(400).json('error getting products'))
}
module.exports = {
handleProducts: handleGetProducts
};
我到处搜索,但找不到任何对我的情况有帮助的东西。
谢谢
一种方法是使用 raw()。我相信这或多或少符合您要获取的查询:
knex.select('id', 'supplier', 'code', 'item', 'price', knex.raw('"price" * "stockonhand" as "stockvalue"'), knex.raw('"stockonhand" - "stockprocessed" as "stockleft"')).table('products')
结果如下:
select "id", "supplier", "code", "item", "price", "price" * "stockonhand" as "stockvalue", "stockonhand" - "stockprocessed" as "stockleft" from "products"
您可以在此处修改 postgres 的确切表达式:QueryLab