sequelize如何查询关联子表的数量
第一种
attributes: ['id', 'serialNumber', 'versionNumber', 'status', 'useTime', 'updatedAt',
[sequelize.literal(`(
SELECT COUNT(*)
FROM slave_devices_record AS slave_devices_record
WHERE
slave_devices_record.slaveDeviceId = SlaveDevice.id
)`),
'recordCount']
]
第二种
let data = await blog.findAll({
attributes: [
'id',
'content'
],
include: [
{
model: 'comment',
as: 'comments',
attributes: [
[Sequelize.fn('COUNT', Sequelize.col('id')), 'comments']
]
}
]
})