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

标签 mysql sql node.js sequelize.js coalesce

我有以下查询:

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/

相关文章:

php - 如何使用 laravel5 从数据 mysql 中提取 json 数据作为对象?

node.js - 使用expressjs时highcharts-export-server出错

node.js - 当数据存储在csv文件中时在Nodejs中响应查询

mysql - SQL : number different dates within users

java - 语法错误插入语句java

node.js - Heroku Node.js 应用程序上的 Socket.io 和多​​个 Dyno。 WebSocket 在连接建立之前关闭

php - 不会删除 Colorbox 中的数据库行(消息)

php - 如何在 php 中使用 fpdf 打破 mutlicell 中的行?

php - 在两个数据库上运行查询

sql - 授予对 View 的 SELECT 权限,但不授予对基础对象的 SELECT 权限