0

Database structure

Table 'Org_f_a_list'

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

Table 'Users'

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

Table 'Users_pa'

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

My current query

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

What I want the result to be

 user_id      user_name     rank    date_added    last_login
       1      Testuser       17           date          date

My current result

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

For some reason the group by u.id is not doing anything and I still pull both rows and not just the max rank

Edit : That fixed it. Thanks guys!

3 Answers 3

1

remove rank_solo from group by

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
Sign up to request clarification or add additional context in comments.

Comments

0

Rank Should not be part of group by as it is causing forming of two different groups

Comments

0

You can Remove the Rank_solo from the Group By Like this:

  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

You can Use Having with the same Query

  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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.