Select highest ranked group only
So im trying to make a integration between my XenForo forum and Source Mod (Counter Strike mod). XenForo saves the user groups a member is a member of like this.
| user_id | user_group_id | is_primary |
That means some members is in that table more then 1 time, in the result I want to have one row of every member max and it should containt the users highest ranked group.
Im not very experienced with this kind of sql queries so I would be glad if someone could help me with this.
The groups i want to have in the result is 5-12, 5 is highest ranked and 12 is lowest ranked. All other groups should not be in the result.
A few rows from the 开发者_开发百科table
user_id | user_group_id | is_primary
1 | 2 | 1
1 | 3 | 0
1 | 4 | 0
1 | 5 | 0
2 | 2 | 1
2 | 5 | 0
3 | 2 | 1
4 | 2 | 1
4 | 6 | 0
EDIT: The result I want would look like this If the table only contained the rows above.
user_id | user_group_id
1 | 5
2 | 5
4 | 6
Best regards
Typing this off the top, but it should do the job:
SELECT user_id, ΜΙΝ(user_group_id) AS min_group_id
FROM users
WHERE user_group_id >= 5 && user_group_id <= 12
GROUP BY user_id
select user_id, min(user_group_id)
from xf_user_group_relation
where max_group_id between 5 and 12
group by user_id
精彩评论