gpt4 book ai didi

mysql - 在 node.js 上使用 coalesce 和 sequelize

转载 作者:行者123 更新时间:2023-12-03 22:34:29 35 4
gpt4 key购买 nike

我有以下查询:

select coalesce(round(sum(vdamesatual) / 
(select count(*)
from feriados
where month(data)=month(current_date) and
day(data)<day(current_date) and
diadasemana in(2,3,4,5,6) and
feriadonacional=0 and
uf <> (select distinct uforigem from baseprodutos where Empresa = 'test'))
*
(select count(*)
from feriados
where month(data)=month(current_date) and
day(data)>=day(current_date) and
diadasemana in(2,3,4,5,6) and
feriadonacional=0 and
uf <> (select distinct uforigem from baseprodutos where Empresa = 'test'))),0) as projecao
from baseprodutos
where Empresa = 'test' and
UFDest = 'uf' and
Fabricante = 'sample';
如您所见,我在第一次选择后立即进行了合并,但我无法找到使用 sequelize 编写此内容的正确方法;你有什么建议吗?

最佳答案

我并没有真正遵循您的查询逻辑,但是 Sequelize 中的规则是您可以使用 sequelize.fn() 调用任何函数,因此在 Sequelize 中使用 COALESCE 从具有别名的 2 列中获取值的简单示例如下所示你的 attributes :

[[sequelize.fn('COALESCE', mysql.col('col_name'), mysql.col('col_2_name')), 'alias'], 'another_col']
如果需要,您可以通过执行以下操作包含另一个函数,例如 SUM:
 [[sequelize.fn('COALESCE', sequelize.fn('SUM', (mysql.col('col_name'), mysql.col('col_2_name'))), (some other code here ...)),'alias']]

关于mysql - 在 node.js 上使用 coalesce 和 sequelize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62975816/

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