我有2个型号Shop和Beacon。我必须返回分配给每个商店的总和信标。我知道如何在SQL中执行它:
SELECT Shops.*, (SELECT COUNT(id) FROM Beacons WHERE shopId = Shops.id) FROM Shops
但我有Sequelize的问题,我尝试下面的事情:
models.Shop.findAll({
attributes: ['id', sqz.fn('count', sqz.col('shopId'))],
include: [
{
model: models.Beacon
}
]
});
答案 0 :(得分:0)
此解决方案有效,但返回查询:
SELECT Shop.*, count(shopId) FROM Shops LEFT OUTER JOIN Beacons ON Shop.id = Beacons.shopId