gpt4 book ai didi

mysql - 如何结合SQL查询的where和order

转载 作者:可可西里 更新时间:2023-11-01 08:10:25 25 4
gpt4 key购买 nike

我有一些项目和拨款是通过比赛联系起来的。赠款有一个到期日期。我想按赠款到期日期对项目的匹配项进行排序。

这为我提供了基于到期日期的匹配项:

def self.expires_date_between(from, to)
Match.where(:grant_id => Grant.where("expires_at >= ? and expires_at <= ?", from, to))
end

这会显示基于特定日期范围的匹配项。

def current_matches
range = user.current_period_range
matches.expires_date_between(range[0], range[1])
end

这非常适合根据特定时间段显示赠款,但赠款不按日期顺序显示 (expires_at)。似乎拨款是按创建日期排序的。如何更改我的查询,以便按到期日期的顺序列出赠款?

我试过:

def self.expires_date_between(from, to)
Match.where(:grant_id => Grant.where("expires_at >= ? and expires_at <= ?", from, to).order('expires_at asc'))
end

这不会引发错误,但也不会更改授权的顺序。有什么想法吗?

最佳答案

假设您已经在模型中定义了关联,这应该可行(但尚未经过测试):

Match
.joins(:grant)
.merge(Grant.where("expires_at >= ? and expires_at <= ?", from, to))
.order('expires_at asc')

关于mysql - 如何结合SQL查询的where和order,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36162347/

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