gpt4 book ai didi

mysql - Rails - 如何加入基于单个 View 的两个关系?

转载 作者:行者123 更新时间:2023-11-30 22:56:05 25 4
gpt4 key购买 nike

我在 guide 之后在我的 Rails 应用程序中定义了一个 View

所以,目前我有这个类:

class GoldenResult < ActiveRecord::Base
scope :latest_builds, -> { select('MAX(id) as id').group(:platform_id, :release_id, :configuration_id) }

end

我想执行以下连接:

SELECT * FROM performance_dev.report_golden_results e 
join (SELECT max(id) as 'id'
from performance_dev.report_golden_results
group by platform_id, release_id, configuration_id) s
ON s.id = e.id;

问题是我不知道如何访问 ON 连接变量(即 ON s.id = e.id)

创建此查询的 Rails 常规方法是什么?

谢谢!

最佳答案

我最终使用以下查询解决了这个问题:

GoldenResult.joins("INNER JOIN (SELECT MAX(id) as 'id'
FROM report_golden_results
GROUP BY platform_id, release_id, configuration_id) latest_builds
ON latest_builds.id=report_golden_results.id")

这是一个解决方法,因为我仍然不知道如何访问 JOIN 中的范围,但它确实有效......!

(即 - 在这个解决方案中我根本没有使用范围)。

关于mysql - Rails - 如何加入基于单个 View 的两个关系?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26325098/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com