基本上我想要的是:
select (table2.col1 * sum(table1.col1)) as myAggregate
from table1
join table2 ON table2.id = table1.id
我已经做到这一点,但不知道如何添加乘法:
Table1Model.findAll({
attributes: [[Sequelize.fn('SUM', 'col1'), 'myAggregate']]
include: [Table2Model]
})
答案 0 :(得分:5)
我最终选择了' raw' MySQL的。请注意,这不会与跨数据库兼容。这是我在代码中做的(这是mySQL)
attributes: [
[Sequelize.literal('(`Meetup.MeetupBusinessPercentages`.percentageCut * SUM(MeetupCharges.amount) / 100 )'), 'totalAmount']
],
答案 1 :(得分:0)
我不知道Sequelize文档是否涵盖了这一点,但是您可以使用Sequelize.where完成此操作。您可能需要更改表名,但是大致是这样的:
Table1Model.findAll({
attributes: [
[
Sequelize.where(
Sequelize.col('table2.col1'),
'*',
Sequelize.fn('SUM', Sequelize.col('col1')),
),
'myAggregate',
]
],
include: [Table2Model]
})