2016-01-13 38 views
0

數據庫結構Mysql的組由表示多行

表 'Org_f_a_list'

id org_id user_id date_added 
    1   1   1  2017-01-01 05:05:05 

表 '用戶'

id  username  last_login 
    1  Testuser  2017-01-01 05:05:05 

表 'Users_pa'

id  user_id  summoner_id  rank_solo 
    1   1     1    15 
    2   1     2    17 

我的當前查詢

select max(rank_solo) as rank,last_login,username,o.user_id,date_added 
     from org_f_a_list o 
     join users u on o.user_id = u.id 
     join users_pa as p on u.id = p.user_id 
     where org_id = :org 
     group by u.id,rank_solo,date_added 
     order by rank desc 

我要什麼的結果是

user_id  user_name  rank date_added last_login 
     1  Testuser  17   date   date 

我現在的結果

user_id  user_name rank  date_added last_login 
     1  Testuser  15   date   date 
     1  Testuser  17   date   date 

出於某種原因,該組由u.id沒有做任何事情,我仍然拉都行而不僅僅是最高排名

編輯:修復它。多謝你們!通過

select max(rank_solo) as rank,last_login,username,o.user_id,date_added 
from org_f_a_list o 
join users u on o.user_id = u.id 
join users_pa as p on u.id = p.user_id 
where org_id = :org 
group by u.id,date_added 
order by rank desc 

回答

1

刪除rank_solo應該不是組的一部分,由它引起兩個不同的組

0

排名從組

0

的形成可以從集團通過篩選刪除Rank_solo:

select max(rank_solo) as rank,last_login,username,o.user_id,date_added 
    from org_f_a_list o 
    join users u on o.user_id = u.id 
    join users_pa as p on u.id = p.user_id 
    where org_id = :org 
    group by u.id,date_added 
    order by rank desc 

您可以使用相同的查詢有

select max(rank_solo) as rank,last_login,username,o.user_id,date_added 
    from org_f_a_list o 
    join users u on o.user_id = u.id 
    join users_pa as p on u.id = p.user_id 
    where org_id = :org 
    group by u.id,date_added 
    Having rank_solo = max(ranl_solo) 
    order by rank desc