Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sequelize.js Sequelize中查询结果的双重计数_Sequelize.js - Fatal编程技术网

Sequelize.js Sequelize中查询结果的双重计数

Sequelize.js Sequelize中查询结果的双重计数,sequelize.js,Sequelize.js,我正在处理一个查询,该查询返回用户的姓名、id以及属于该用户的操作和评论的数量。以下是SQL中的查询: SELECT users.id, users.name, ( SELECT count(*) from actions where users.id = actions.user_id ) as Action_Count, ( SELECT count(*) from comments where users.id = comments.user_id ) as Com

我正在处理一个查询,该查询返回用户的姓名、id以及属于该用户的操作和评论的数量。以下是SQL中的查询:

SELECT users.id, users.name, (
    SELECT count(*) from actions where users.id = actions.user_id
  ) as Action_Count, (
    SELECT count(*) from comments where users.id = comments.user_id
  ) as Comment_Count
FROM users
我对续集有点陌生,但经过太多小时的搜索和多次尝试,我似乎不知道如何正确地编写它。所以我希望这里有人能帮助我。这是我最好的尝试:

 User.findAll({
  attributes: ['name','id',
    [Sequelize.fn('count', Sequelize.col('actions.user_id')), 'count_of_actions'],
    [Sequelize.fn('count', Sequelize.col('comments.user_id')), 'count_of_comments']
  ],
  include: [{ attributes: [], model: Action },{ attributes: [], model: Comment }],
  group: ['user.id'],
  order: [['createdAt', 'ASC']]
})
但是,如果有任何注释,这将为
Action\u Count
Comment\u Count
返回相同的数字(两者相乘)


非常感谢任何帮助或指导

我今天也面临同样的问题。下面是我通过添加不同的函数来解决问题的答案:

User.findAll({
属性:['name','id',
[Sequelize.fn('count',Sequelize.fn('DISTINCT',Sequelize.col('actions.user_id')),'count_of_actions'],
[Sequelize.fn('count',Sequelize.fn('DISTINCT',Sequelize.col('comments.user_id')),'count_of_comments']
],
包括:[{attributes:[],model:Action},{attributes:[],model:Comment}],
组:['user.id'],
订单:['createdAt','ASC']]
})

这篇文章很老了,我希望这能帮助别人。

我今天也面临同样的问题。下面是我通过添加不同的函数来解决问题的答案:

User.findAll({
属性:['name','id',
[Sequelize.fn('count',Sequelize.fn('DISTINCT',Sequelize.col('actions.user_id')),'count_of_actions'],
[Sequelize.fn('count',Sequelize.fn('DISTINCT',Sequelize.col('comments.user_id')),'count_of_comments']
],
包括:[{attributes:[],model:Action},{attributes:[],model:Comment}],
组:['user.id'],
订单:['createdAt','ASC']]
})

这篇文章很老了,我希望这会对别人有所帮助。

我使用了
sequelize.query
来运行上面的sql查询,效果很好。如果可能的话,我仍然很好奇如何使用sequelize语法来实现这一点。我使用了
sequelize.query
来运行上面的sql查询,这很有效。如果可能的话,我仍然很好奇如何使用sequelize语法来实现这一点。