【发布时间】:2019-02-07 17:51:49
【问题描述】:
例如说我有关系:
Posts has_many Comments
我正在尝试按照以下方式做一些事情:
Post |> Repo.aggregate(:count, :comments)
但是,Ecto 抱怨 :cmets 是一个虚拟字段,因此它无法计算它。有什么好的方法可以解决这个问题?
【问题讨论】:
例如说我有关系:
Posts has_many Comments
我正在尝试按照以下方式做一些事情:
Post |> Repo.aggregate(:count, :comments)
但是,Ecto 抱怨 :cmets 是一个虚拟字段,因此它无法计算它。有什么好的方法可以解决这个问题?
【问题讨论】:
我假设您想要一组帖子的评论计数。如果您想要所有帖子的评论计数,您可以省略 where 子句。
post_ids = [1, 2, 3]
Comment
|> where([c], c.post_id in ^post_ids)
|> group_by([c], c.post_id)
|> select([c], {c.post_id, count("*")})
|> Repo.all()
这将在给定 post_ids 的情况下按帖子对 cmets 进行分组,并计算每个 cmets 的数量。它将返回一个包含元组的列表,例如像这样
[
{1, 10},
{2, 3},
{3, 5}
]
如果帖子没有 cmets,它将不会在结果集中列出。
【讨论】:
这是我的最终解决方案,link has_many clicks
def list_links_count do
query =
from l in Link,
join: c in Click, as: :click,
where: c.link_id == l.id,
group_by: l.id,
select: {l, count(l.id)}
query |> Repo.all
end
【讨论】: