Node Sequelize order二级排序

421 阅读1分钟

说明

二级排序很简单,及在order里面多写一个数组即可。

代码

      const { count, rows } = await Goal.findAndCountAll({
        include: {
          model: Plan,
          attributes: ["id", "content"],
        },
        attributes: ["id", "content", "star", "end_date", "status", "user_id", "created_at"],
        where: whereOption,
        order: [
          ["star", "DESC"], // 一级 降序
          ["created_at", "DESC"], // 二级 降序
        ],
        offset: (pageNo - 1) * pageSize,
        limit: pageSize,
        distinct: true,
      });

注意

涉及到include,假如要对created_at或createdAt排序的话,那么主模型的attributes里面要返回created_at或createdAt,如上代码所示。