【问题标题】:How can I get calculated value with different two column on PostgreSQL + Rails?如何在 PostgreSQL + Rails 上获得不同两列的计算值?
【发布时间】:2020-02-16 14:30:26
【问题描述】:

这是桌子

  • 标题
  • post_count
  • comment_count

我想得到 post_count 和 comment_count 的总和。 我做的就是这样,但是不行。

.select("count(tables.post_count + tables.comment_count) AS field_name").where("field_name >= ?", 3)

【问题讨论】:

  • 你能提供一个简单的例子来说明你的表格是什么样的,以及你的预期结果吗?
  • 例如摘要表有这些列。 “post_count”,是整数,它有总帖子数 “comment_count”,它是整数,它有总评论数我想要post_count + comment_count的总和
  • 我根据我的理解添加了一个答案,我会让你检查这是否是你要找的。​​span>

标签: ruby-on-rails postgresql activerecord


【解决方案1】:

试试这样的

# Assuming your model is named Table
Table.
  select(:id, 'post_count + comment_count AS field_name').
  where('post_count + comment_count > 3') # Condition you added at the end of your question

这将返回带有两个字段的 ActiveRecords:

  • 记录ID
  • “field_name”列

但我不完全确定这就是你要找的东西。

【讨论】:

  • 如果它解决了您的问题@esmanbo 中所述的问题,请考虑接受答案。
猜你喜欢
  • 2021-09-26
  • 2011-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-04
  • 2021-08-05
  • 1970-01-01
相关资源
最近更新 更多