and thank for the answers.
i use nodejs / sequelize / mysql
this is my problem:
i have table client like this:
id username age name
----------------------
12 seveun 25 john |
----------------------
10 superlol 12 johny |
----------------------
the table client has one to many relation with statistic table
the statistic table seem like this :
id gain profit clientId
-------------------------
12 22 25 12 |
-------------------------
10 34 12 12 |
-------------------------
10 34 12 10 |
-------------------------
i want this result :
{
client: [
{
'username': 'seveun',
'statistic': [total: 2]
},
{
'username': 'superlol'
'statistic': [total: 1]
}
]
}
i test this sequelize code :
await ClientModel.findAll({
include: [
{
as: 'statistic',
model: StatisticModel,
attributes: [
[[Sequelize.fn("COUNT", Sequelize.col("statistic.id")), "total"]]
],
},
],
});
but the count, count every statistic, not statistic for each parent
my result is :
client: [
{
'username': 'seveun',
'statistic': [total: 3]
}
thanks a lot for your help