如何通过 sequelize 获得行的不同值?

新手上路,请多包涵

我有有价值的桌子

id country
1  india
2  usa
3  india

我需要使用 sequelize.js 从国家列中找到不同的值

这是我的示例代码…

 Project.findAll({

    attributes: ['country'],
    distinct: true
}).then(function(country) {
     ...............
});

有什么方法可以找到 distict 值

原文由 made_in_india 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 503
2 个回答

试试这个: https ://github.com/sequelize/sequelize/issues/2996#issuecomment-141424712

 Project.aggregate('country', 'DISTINCT', { plain: false })
.then(...)

原文由 Maria Ines Parnisari 发布,翻译遵循 CC BY-SA 3.0 许可协议

您可以使用 Sequelize.fn

 Project.findAll({
    attributes: [
        // specify an array where the first element is the SQL function and the second is the alias
        [Sequelize.fn('DISTINCT', Sequelize.col('country')) ,'country'],

        // specify any additional columns, e.g. country_code
        // 'country_code'

    ]
}).then(function(country) {  })

原文由 djones 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题