sql - 如何将此 SQL 查询转换为 sequelize?

标签 sql sequelize.js

SELECT
  "flatNo",
  "buildingId",
  count(*)
FROM "FlatDetails"
WHERE "buildingId"= 'E7EA2771'
GROUP BY
  "flatNo",
  "buildingId"
HAVING count(*) > 1;

我试过这个工具 https://pontaku-tools.com/english/ 但它没有给我正确的答案。

最佳答案

你可以试试这个代码。关键字是 fn & literal

FlatDetails.findAll({
    attributes: ['flatNo', 'buildingId', [sequelize.fn('count', sequelize.col('*')), 'count']],
    where: {
        buildingId: 'E7EA2771'
    },
    group: ['flatNo', 'buildingId'],
    having: sequelize.literal('count(*) > 1'),
    raw: true,
    logging: true
})
.then(flats => {
    console.log(flats);
})
.catch(err => {
    console.log(err);
})

关于sql - 如何将此 SQL 查询转换为 sequelize?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60276787/

相关文章:

sql - pl/python 接收和输出一组行

MySQL - 复杂度 : SELECT COUNT(*) FROM MyTable;

mysql - Sequelize - 原始 :true and raw:false 和有什么区别

javascript - Sequelize js 'include' 和 'raw'

SQL - 在同一查询中填充多个变量

java - 更新 hibernate 中的查询

sql - 奇怪的 : Planner takes decision with lower cost, 但(非常)查询运行时间长

node.js - 跟踪不产生结果的 WHERE 子句值

node.js - 如何解决 "sequelize: command not found"?

sequelize.js - 如何在 Sequelize 中使用 MySQL JSON 数据类型