Mysql组通过显示多行

时间:2016-01-13 18:42:41

标签: 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的团队没有做任何事情,我仍然拉两行而不仅仅是最高等级

编辑:修复了它。多谢你们!

3 个答案:

答案 0 :(得分:1)

群组中删除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

答案 1 :(得分:0)

排名不应成为小组的一部分因为它导致形成两个不同的小组

答案 2 :(得分:0)

您可以从Group中删除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