User.findAll({ attributes: [ 'User.username', [sequelize.literal('(SELECT COUNT(*) FROM Posts WHERE Posts.userId = User.id)'), 'PostCount'] ], order: [[sequelize.literal('PostCount'), 'DESC']] });
来源:https://www.cnblogs.com/xiguachaodan/p/11854960.html