2014-09-22 96 views
-1

后,我有以下的数据库组:Ruby on Rails的 - 按订单

ID | User | Item | Score | Time | 
1 | 1 | 1 | 10 | 5 | 
2 | 1 | 1 | 9  | 95 | 
3 | 1 | 1 | 50 | 30 | 
4 | 1 | 1 | 5  | 10 | 

我想要得到的最高分然后最低的时间和GROUP_BY用户。 总之我只想:

3 | 1 | 1 | 50 | 30 | 

我设法拿到订单做做:

order(score: :desc, time: :asc) 

ID | User | Item | Score | Time | 
3 | 1 | 1 | 50 | 30 | 
1 | 1 | 1 | 10 | 5 | 
2 | 1 | 1 | 9  | 95 | 
4 | 1 | 1 | 5  | 10 | 

但是当我添加GROUP_BY它没有显示我的第一道防线。


这里是真正的数据:

irb(main):029:0* Game.all.each { |g| p g.attributes } 
Game Load (0.0ms) SELECT "games".* FROM "games" 
{"id"=>18, "gift_id"=>13, "user_id"=>1, "time"=>0.04, "score"=>3} 
{"id"=>19, "gift_id"=>13, "user_id"=>1, "time"=>5.0, "score"=>8} 
{"id"=>20, "gift_id"=>13, "user_id"=>1, "time"=>50.0, "score"=>55} 
{"id"=>21, "gift_id"=>13, "user_id"=>1, "time"=>56.0, "score"=>33} 
{"id"=>22, "gift_id"=>13, "user_id"=>1, "time"=>9.0, "score"=>23} 
{"id"=>23, "gift_id"=>13, "user_id"=>1, "time"=>12.0, "score"=>21} 
{"id"=>24, "gift_id"=>13, "user_id"=>1, "time"=>51.0, "score"=>55} 

我要的结果是id 20,因为它有最好的得分(55)和ID 20和24

之间的最短时间
irb(main):035:0* Game.group('user_id').having("max(score) = score and min(time) = time") 
    Game Load (0.0ms) SELECT "games".* FROM "games" GROUP BY user_id HAVING max(score) = score and min(time) = time 
=> #<ActiveRecord::Relation []> 

编辑: 看来我解决我的概率LEM用这个命令:

Game.order("score DESC, time ASC").group(:user_id) 

我不明白为什么我从来没有尝试过这一个或为什么它永远不会奏效之前...

+0

什么是您的型号名称? – 2014-09-22 18:30:52

+0

游戏。真正的命令是:'games.order(score::desc,time :: asc)' – 2014-09-22 18:34:15

+0

@ZazOufUmI和group? – 2014-09-22 18:52:36

回答

1

我对最终输出有点糊涂了。但我将一些数据值添加到型号Game。我尝试看起来像如下: -

2.1.2 :003 > Game.all.each { |g| p g.attributes } 
# Game Load (0.5ms) SELECT "games".* FROM "games" 
# => {"id"=>1, "user_id"=>1, "item_id"=>1, "score"=>10, "times"=>5, "created_at"=>Mon, 22 Sep 2014 18:44:43 UTC +00:00, "updated_at"=>Mon, 22 Sep 2014 18:46:45 UTC +00:00} 
# => {"id"=>2, "user_id"=>1, "item_id"=>1, "score"=>10, "times"=>5, "created_at"=>Mon, 22 Sep 2014 18:44:43 UTC +00:00, "updated_at"=>Mon, 22 Sep 2014 18:46:50 UTC +00:00} 
# => {"id"=>3, "user_id"=>1, "item_id"=>1, "score"=>50, "times"=>3, "created_at"=>Mon, 22 Sep 2014 18:44:43 UTC +00:00, "updated_at"=>Mon, 22 Sep 2014 18:56:44 UTC +00:00} 
# => {"id"=>4, "user_id"=>1, "item_id"=>1, "score"=>5, "times"=>10, "created_at"=>Mon, 22 Sep 2014 18:44:43 UTC +00:00, "updated_at"=>Mon, 22 Sep 2014 18:47:26 UTC +00:00} 

2.1.2 :004 > Game.group('user_id').having("max(score) = score and min(times) = times") 
# => #<ActiveRecord::Relation [#<Game id: 3, user_id: 1, item_id: 1, score: 50, times: 3, created_at: "2014-09-22 18:44:43", updated_at: "2014-09-22 18:56:44">]> 

现在,看到上面查询选择该行,其中有最大score最低times

+0

感谢您的回答,但您的命令返回给我nothings:/我用真实数据更新我原来的问题,请看看 – 2014-09-22 20:43:46

+0

@ZazOufUmI我会尽快回复您..但您首先检查我的数据..然后是您的数据。我看到你有_blank关系_ ..你应该得到_blank_。你检查一下,你发现记录是**最大**'分数',那么'时间'不是最小**。这就是为什么你有_blank关系_ ..逻辑没有错。 – 2014-09-23 06:00:24

+0

好的,我现在看到问题了。但是,我该怎么做才能完成订单**首先是**,然后是**。如果我用英语翻译它,它会是:我想要列表顶部的最佳分数,如果有多个最佳分数(在我的示例中为20和24),我希望最低的时间在第一位。 – 2014-09-23 07:09:42