【问题标题】:Sequelize findAll where a column value is the same as at least one other item in tableSequelize findAll,其中列值与表中的至少一个其他项目相同
【发布时间】:2017-12-27 12:31:14
【问题描述】:

我目前有以下查询,它正在获取具有 id = 某个值的所有内容,但我需要它来仅获取具有其列(称为 Uid)值的项目在表中出现多次。

基本上我需要从以下查询的结果中过滤掉具有唯一 Uid 列值的项目

models.table.findAll({
    where:{
        id: req.params.id
    }
})

【问题讨论】:

    标签: mysql node.js express sequelize.js


    【解决方案1】:

    所以我相信您正在寻找的 SQL 查询会是

    SELECT id, uid, count(*) FROM table
    WHERE id = :id
    GROUP BY id, uid
    HAVING count(uid) > 1;
    

    在 Sequelize 中会是:

    models.table.findAll({
        where: { id: req.params.id },
        group: ['id', 'uid'],
        having: [sequelize.literal('count(?) > ?)', 'uid', 1] 
    })
    

    不完全确定这是否是 Sequelize 的正确语法。另请查看sequelize.fn()sequelize.col()

    【讨论】:

      猜你喜欢
      • 2021-07-16
      • 1970-01-01
      • 1970-01-01
      • 2021-09-26
      • 2013-05-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-14
      相关资源
      最近更新 更多