Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
751 views
in Technique[技术] by (71.8m points)

sequelize mysql group 查询获各分组最后一条

sequlize通过聚合查询:

const res = await ctx.mode.Record.findAndCountAll({
    order: [['createdAt', 'DESC']],
    group: ['message']
})

但结果是每个组个的第一条,怎么设置才会让group取最后一条呢?

查了下mysql 是 先把数据排序,再查询group。

sequelize不知道怎么?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
const res = await ctx.mode.Record.findAndCountAll({
    order: [[sequelize.fn('max', sequelize.col('createdAt')), 'DESC'],],
    group: ['message']
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...