Counting associated entries with Sequelize(使用 Sequelize 计算关联条目)
问题描述
我有两个表,locations 和
p>sensors
.sensors
中的每个条目都有一个指向 locations
的外键.使用 Sequelize,我如何从 locations
中获取所有条目以及 sensors
中与 locations
中的每个条目相关联的条目总数?
原始 SQL:
型号:
使用 findAll()
与 include()
和 sequelize.fn()
COUNT
的代码>:
或者,您可能还需要添加 group
:
I have two tables, locations
and sensors
. Each entry in sensors
has a foreign key pointing to locations
. Using Sequelize, how do I get all entries from locations
and total count of entries in sensors
that are associated with each entry in locations
?
Raw SQL:
Models:
Use findAll()
with include()
and sequelize.fn()
for the COUNT
:
Or, you may need to add a group
as well:
这篇关于使用 Sequelize 计算关联条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!