具有多个字段的MySql排序查询

时间:2016-11-16 05:32:41

标签: mysql ruby-on-rails sorting

我有两张表“activity_stats”& “activity_stats_values”

activity_stats table
# id, activity_id, kyf_id, kyf_sort
618      84          5       1
619      84          6       2
638      84          4       3 

activity_stats_values table
# id, activity_id, player_id, kyf_id, value
2563    84          46          5       45
2564    84          46          6       60
2587    84          47          5       10
2588    84          47          6       25
2589    84          49          5       10
2590    84          49          6       40
2591    84          48          5       30
2592    84          48          6       15
2594    84          46          4       NULL
2595    84          47          4       80
2596    84          48          4       NULL
2597    84          49          4       NULL

要求

玩家应按降序排列值。意味着第一个关键字值最高的玩家(基于表“activity_stats”的字段“kyf_sort”的kyf_id,kyf_id位置)应该在顶部(然后是第二个关键值,然后是第三个关键值)。

预期输出

# id, activity_id, player_id, kyf_id, value  kyf_sort
 2563   84          46          5       45      1
 2564   84          46          6       60      2
 2594   84          46          4       NULL    3

 2591   84          48          5       30      1
 2592   84          48          6       15      2
 2596   84          48          4       NULL    3

 2589   84          49          5       10      1
 2590   84          49          6       40      2
 2597   84          49          4       NULL    3

 2587   84          47          5       10      1
 2588   84          47          6       25      2
 2595   84          47          4       80      3

按顺序播放ID [46,48,49,47]

I tried the following query 
SELECT    ac_st_v.activity_id,ac_st_v.player_id,ac_st_v.value,ac_st.kyf_id,ac_st.kyf_sort,pl.first_name ,
(select max(value)  
 from activity_stats_values 
  where activity_id=ac_st_v.activity_id 
     and kyf_id=ac_st.kyf_id 
 group by activity_id) as m_value
FROM teamplayer.activity_stats_values as ac_st_v
 JOIN teamplayer.activity_stats as ac_st
  ON ac_st_v.activity_id=ac_st.activity_id 
   AND  ac_st_v.kyf_id=ac_st.kyf_id
 JOIN teamplayer.players as pl
  ON ac_st_v.player_id=pl.id
where ac_st_v.activity_id= 84  
order by  ac_st_v.player_id,ac_st.kyf_sort,m_value

有没有办法对像这样的值进行排序?

0 个答案:

没有答案