我有一个 SQL 查询(使用 mysql 作为 DB),我现在需要将其重写为 node.js 中的 sequelize.js 查询。
SQL查询
SELECT p.UserID, SUM(p.score), u.username
FROM Picks p
LEFT JOIN Users u
ON p.UserId = u.id
GROUP BY p.UserId;
不太确定需要如何构造此查询才能使用 sequelize 获得相同的结果。
最佳答案
这应该做你需要的:
db.Pick.findAll({
attributes: [
'UserID',
[db.sequelize.fn('SUM', db.sequelize.col('score')), 'score']
],
include: [{
model: db.User,
required: true,
attributes: ['username']
}],
group: ['UserID']
}).then((results) => {
...
})
关于sql - 如何为 Sequelize 转换 SQL 查询?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47878505/