gpt4 book ai didi

javascript - 添加 knex.js .andWhere() 条件为 true

转载 作者:行者123 更新时间:2023-12-02 23:19:21 24 4
gpt4 key购买 nike

我有这个 Knex 查询:

const customerProducts = db.sequelize.knex.select([
'CustomerProduct.id AS _id',
'CustomerProduct.last_delivered AS _lastDelivered',
'CustomerProduct.margin AS _margin',
'CustomerProduct.outlier AS _outlier',
'CustomerProduct.growth AS _growth',
'CustomerProduct.period AS _period',
'CustomerProduct.price AS _price',
'CustomerProduct.active AS _active',
'CustomerProduct.customer_id AS _customerId',
'CustomerProduct.product_id AS _productId',
'CustomerProduct.modified AS _modified',
'CustomerProduct.month_value AS _monthValue',
'customer.id AS _customer_id',
'customer.title AS _customer_title',
'customer.code AS _customer_code',
])
.from('customer_products AS CustomerProduct')
.innerJoin(
'customers AS customer',
'CustomerProduct.customer_id',
'customer.id',
)
.where(whereClause)
.limit(limit)
.offset(offset);

我想包含一个 .andWhere() 选项,该选项仅在满足此条件时添加到查询中:

if (overdue === 'true')

我该怎么做?

最佳答案

你可以这样做:

const query = db.sequelize.knex.select([
'CustomerProduct.id AS _id',
'CustomerProduct.last_delivered AS _lastDelivered',
'CustomerProduct.margin AS _margin',
'CustomerProduct.outlier AS _outlier',
'CustomerProduct.growth AS _growth',
'CustomerProduct.period AS _period',
'CustomerProduct.price AS _price',
'CustomerProduct.active AS _active',
'CustomerProduct.customer_id AS _customerId',
'CustomerProduct.product_id AS _productId',
'CustomerProduct.modified AS _modified',
'CustomerProduct.month_value AS _monthValue',
'customer.id AS _customer_id',
'customer.title AS _customer_title',
'customer.code AS _customer_code',
])
.from('customer_products AS CustomerProduct')
.innerJoin(
'customers AS customer',
'CustomerProduct.customer_id',
'customer.id',
);
if (overdue === 'true') {
query = query.where(whereClause)
}
const customerProducts = query.limit(limit).offset(offset);

关于javascript - 添加 knex.js .andWhere() 条件为 true,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57019400/

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