首页 > 解决方案 > 在 node.js 上使用合并和续集

问题描述

我有以下查询:

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 编写它;你有什么建议吗?

标签: mysqlsqlnode.jssequelize.jscoalesce

解决方案


我并没有真正遵循您的查询逻辑,但是 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']]

推荐阅读