NodeJS 如何在sequelize中使用GROUP BY YEAR(created_at

sf6xfgos  于 2023-03-01  发布在  Node.js
关注(0)|答案(1)|浏览(144)

如何在***sequelize*for mysqlGROUP BY YEAR(创建于)**中使用分组
我正在尝试这样做,但它显示一个错误

const result = await arbcase.findAll({
    attributes: [[arbcase.sequelize.literal(`COUNT(*)`), "count"]],
    group: ["YEAR(created_at)"],
  });

错误SQL消息:“'group语句'中存在未知列'YEAR(created_at)',

'选择计数(*)作为countarbcase作为arbcase分组为YEAR(created_at);“

yjghlzjz

yjghlzjz1#

const result = await arbcase.findAll({
  attributes: [
    "created_at",
    [arbcase.sequelize.literal(`COUNT(*)`), "count"],
  ],
  group: [
    arbcase.sequelize.fn("YEAR", arbcase.sequelize.col("created_at")),
    arbcase.sequelize.fn("MONTH", arbcase.sequelize.col("created_at")),
  ],
  // SELECT created_at ,count(*) FROM `arbcase` GROUP BY YEAR(created_at), MONTH(created_at) ASC
});

这个代码对我有用

__your__mode__name__.sequelize.col("created_at"))

此函数用于Sequelize中mwsql中的每个函数

相关问题