NodeJS 使用sequelize ORM进行多对多表排序

o75abkj4  于 2023-03-17  发布在  Node.js
关注(0)|答案(2)|浏览(131)

我在用户和分支之间建立了多对多关系

return Branch.findAll({
        where: {
            void: false,
            active: true,
        },
        include : {
            model: User,
            as: "user",
            where: {
                active: true,
                void: false,
            },
            required: false,
        },
        order: [
            1st order ==> order by branch name
            2nd order ==> order by user name

        ],
    })

我怎样才能写一个查询呢?

enxuqcxy

enxuqcxy1#

是的,您可以在sequelize中使用多个订单列,如:

order: [
   ['branch_name', 'ASC'], // order by ASCENDING by branch name column
   ['user.name', 'ASC'] // order by ASCENDING by user name column
]

希望这能有所帮助。

nhhxz33t

nhhxz33t2#

我能够通过此查询得到答案

return Branch.findAll({
    where: {
        void: false,
        active: true,
    },
    include : {
        model: User,
        as: "user",
        where: {
            active: true,
            void: false,
        },
        required: false,
    },
    order: [
         ['branch_name', 'ASC'],
         [{model:User,as:"user"},'name','ASC']

    ],
})

链接:Sequelize documentation

相关问题