代码之家  ›  专栏  ›  技术社区  ›  Chance

根据Sequelize中的字段拉入特定记录/行

  •  0
  • Chance  · 技术社区  · 2 年前

    因此,我正在处理的一个基本Sequelize查询(为了简单起见,减去fluff)如下所示:

    { model: Contact,
        required: true,
        attributes: ['FullName', 'Country'],
            include: [
                {
                model: Rank,
                required: false,
                attributes: ['Year', 'Category', 'Rank'],
                    where: {
                        Year: 2021
                        }
                }
    ]
    

    问题是,我只需要 排名靠前的 (金、银、铜或无等级/空)行,即使我们的联系人在等级表中有多行,如果他们有多个类别

    因此,使用上述逻辑,我得到如下数据:

    "FullName": "Bob",
    "Country": 'USA',
     "Rank": [
                {
                    "Year": 2021,
                     "Category": "ExampleA",
                     "Rank": "Silver"
                    },
                    {
                        "Year": 2021,
                        "Category": "ExampleB",
                        "Rank": "Gold"
                    }
                ]
    

    有没有办法让Sequelize只在上例中的“Gold”行中划入,因为这是最高的排名?

    因此,该特定用户的期望结果是:

    "FullName": "Bob",
    "Country": 'USA',
    "Rank": [
            {
                {
                    "Year": 2021,
                    "Category": "ExampleB",
                    "Rank": "Gold"
                }
            ]
    

    若一个用户有银和铜,但并没有金,那个么理想的结果就是只拉进银排。

    非常感谢您的任何见解,如果需要的话,我会提供更多信息,我一直在为这件事发愁。

    0 回复  |  直到 2 年前