轨道与条件不同

发布于 2025-02-04 00:15:49 字数 396 浏览 3 评论 0原文

我有四个数据表,这些表重复了状态,但具有不同的创建_at(假设它是在一个名为“测试”的相关模型上)

样本:

column 1: status_type : "test", created_at: "2019-08-14"

column 2: status_type : "test", created_at: "2020-20-14"

column 3: status_type : "integer", created_at: "2021-20-15"

column 4: status_type : "integer", created_at: "2022-22"15"

我想获得两个数据,这是测试和整数,但我需要一个是最新更新的数据或谁是根据他们的创建_at的最新消息。如何查询这个?谢谢

I have four table in data which have repeated status but with different created_at (assuming it is on an associated model named "test")

Sample:

column 1: status_type : "test", created_at: "2019-08-14"

column 2: status_type : "test", created_at: "2020-20-14"

column 3: status_type : "integer", created_at: "2021-20-15"

column 4: status_type : "integer", created_at: "2022-22"15"

I want to get two data which is test and integer but i need the one who is the most updated or who is more recent based on their created_at. How to query this? Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

最冷一天 2025-02-11 00:15:49

你可以做以下

Class.where(status_type: ['test', 'integer']).order('created_at').select('DISTINCT ON (status_type) *')

You can do the following

Class.where(status_type: ['test', 'integer']).order('created_at').select('DISTINCT ON (status_type) *')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文