Here's my mysql table:
Table League (userid and lid are primary keys):
*userid* *lid* rank win loss streak score
---------------------------------------------------------
2 1 1 2 0 2 10
2 3 2 1 1 1 5
5 1 2 1 1 1 5
I'm trying to select the users with the top score only once. For example since userid 2 is in league (lid) 1 and 3, only his top score will be selected in the query. So in that case the row with score 10 would be selected since that's the users top score from both league 1 and 3. The row with lid 3 will not be selected.
So the query results should look like this:
userid lid rank win loss streak score
---------------------------------------------------------
2 1 1 2 0 2 10
5 1 2 1 1 1 5
As you can see userid 2 with lid 3 was not in the result because the score 10 from lid 1 was grater than score 5 from league 3. Any ideas?
SELECT l.userid, u.username, l.lid, l.rank, l.win, l.loss, l.streak, l.score
FROM (SELECT userid, MAX(score) AS MaxScore
FROM League
GROUP BY userid) q
INNER JOIN League l
ON q.userid = l.userid
AND q.MaxScore = l.score
INNER JOIN users u
ON l.userid = u.userid
SELECT *
FROM table t1
WHERE t1.score = (SELECT MAX(t2.score)
FROM table t2
WHERE t1.userid = t2.userid)
This will display ties, don't know if you want those or not.
Here the simplest solution:
SELECT *
FROM League t1
WHERE
t1.lig = (SELECT t2.lig
FROM League t2
WHERE t2.userid = t1.userid
ORDER BY score desc
LIMIT 1
)
Related
I need to select the users who have won at least 1 round in all of the different competitions.
I have the following table sctructure:
results
id
round_id
user_id
result
1
1
2
1
2
1
1
2
rounds
id
category_id
1
1
2
1
3
2
4
2
categories championship
id
competition_id
1
1
2
2
competitions team
id
competition_name
1
Competition A
2
Competition B
Now let's say Bob has won at least 1 round in both Competition A and B each, he needs to show up in the list. But Joe, who's won 1 round in Competition A but nothing in Competition B, must not show up.
I tried writing a script like this, but I can see the flaw in my logic. It's looking for a row where the round_id is both 1 and 2 is impossible.
SELECT user_id FROM results WHERE
(result = 1 AND round_id IN
(SELECT id FROM rounds WHERE category_id IN
(SELECT id FROM categories WHERE competition_id = 7)
)
) AND
(result = 1 AND round_id IN
(SELECT id FROM rounds WHERE category_id IN
(SELECT id FROM categories WHERE competition_id = 8)
)
) AND
(result = 1 AND round_id IN
(SELECT id FROM rounds WHERE category_id IN
(SELECT id FROM categories WHERE competition_id = 9)
)
)
GROUP BY driver_id
How can I achieve this?
Join results to rounds and categories (competitions is not needed because competition_id exists in categories).
Then filter the rows for only the users who won at least one round and aggregate by user with the condition in the HAVING clause for the user that all 3 competition_ids are returned:
SELECT rs.user_id
FROM results rs
INNER JOIN rounds rd ON rd.id = rs.round_id
INNER JOIN categories cg ON cg.id = rd.category_id
WHERE rs.result = 1 AND cg.competition_id IN (7, 8, 9)
GROUP BY rs.user_id
HAVING COUNT(DISTINCT cg.competition_id) = 3;
I have two tables groups and groups_members
groups
id
name
1
GROUP 1
2
GROUP 2
3
GROUP 2
groups_members
group_id
user_id
1
123
2
123
2
555
1
4643
3
45434
Now I want to display ALL groups and check if the user i show the groups for (lets say user_id = 555) is in a group or not. Something like this:
SELECT g.id, g.name, is_in_group
FROM g.groups
JOIN groups_members gm ON gm.user_id = 555
Expected output:
id
name
is_in_group
1
GROUP 1
NULL
2
GROUP 2
1
3
GROUP 3
NULL
You have several ways to do so, a simple trick that comes to my mind is this one:
SELECT
g.*,
CAST(gm.user_id / gm.user_id AS UNSIGNED) AS is_in_group
FROM sgroups AS g
LEFT JOIN sgroups_members AS gm
ON g.id = gm.group_id
AND gm.user_id = 555;
-- id name is_in_group
-- 1 GROUP 1 NULL
-- 2 GROUP 2 1
-- 3 GROUP 3 NULL
Database structure
Table 'applicants'
id org_id team_id
1 1 1
Table 'teams'
id name
1 Test
Table 'teams_members'
id team_id user_id
1 1 1
2 1 2
Table 'users_playeraccounts'
id user_id summoner_id rank_solo
1 1 1 5
2 1 2 8
3 2 3 7
select sum(rank_solo) as rank_sum,
max(rank_solo) as highest_rank,
count(tt.id) as members,
t.name,
o.team_id
from applicants o
join teams t on o.team_id = t.id
join teams_members tt on t.id = tt.team_id
join users_playeraccounts p on tt.user_id = p.user_id
where org_id = :org
group by team_id
This offcourse gives me a result like
rank_sum highest_rank members name team_id
20 8 3 Test 1
Is there a way for me to get both the count of members with their playeraccounts aka
If 1 user has 2 it'll be 2
And also a way for me to keep it as 1 so it literally just counts the rows found in teams_members neglecting the entries in users_playeraccounts?
I want to receive both 2 and 3 as a result of my query.
You want to count the distinct number of entries in tt.id, so you can do that like this:
SELECT ... COUNT(DISTINCT tt.id) AS distinct_members ...
Rather than giving you a count of every row that has a non-null tt.id, you'll get a count of the number of unique values.
This is a follow on from this question.
I want to get the game id that a player had his maximum score in. I am struggling to get the corresponding game_id column value when selecting the max(score)
My table is as follows:
id game_id player_id score
1 1 1 345
2 1 2 234
3 2 1 190
4 2 2 167
5 3 4 230
6 3 1 230
7 4 2 453
8 4 3 230
My query looks like this so far:
SELECT s.id, t.game_id, t.score
FROM (
SELECT game_id, score
FROM stats
WHERE player_id =2
) t
LEFT JOIN stats s on s.game_id = t.game_id AND s.score = t.score
WHERE s.player_id = 2
This gives me
id game_id score
2 1 234
4 2 167
7 4 453
I then need to join this whole query on as a subquery on the maximum score (id = 7), in order to then get the game_id, but I am not sure how to say join on this maximum value
here i have created sql feedle you can remove
SELECT s.id, t.game_id, t.score
FROM (
SELECT game_id, score
FROM game
WHERE player_id =2 order by score desc limit 0,1
) t
LEFT JOIN game s on s.game_id = t.game_id AND s.score = t.score
SELECT a.*
FROM TableName a
INNER JOIN
(
SELECT player_ID, MAX(Score) max_score
FROM TableName
GROUP BY Player_ID
) b ON a.Player_ID = b.player_ID AND
a.Score = b.max_score
Hope this can help you.
select game_id, player_id, score
from (
select rank() over (partition by player_id order by score desc) as rank_id, game_id, player_id, score
from stats
) a
where rank_id=1 and player_id = 2
I have four tables: groups, users, votes, follows.
The structures of these tables are
groups
g_id g_title g_content
1 t1 content1
2 t2 content2
users
u_id u_groupid
1 1
2 1
3 2
4 2
5 2
votes
v_id v_userid v_groupid v_votes
1 1 1 1
2 1 2 1
3 2 2 1
4 3 2 1
5 3 1 1
follows
f_id f_userid f_groupid
1 1 1
2 2 1
3 2 2
4 3 1
5 3 2
The groups table records the basic information of a "group".
The users table keeps the relationship between users and groups, that is, if the user A belongs to groups B, then there will be a record in the user table.
The votes table means the supportive attitude that a user holds to a group.
If user A is interested in group A, then add a entry into the Follows table.
Now my problem is how to write one select statement to query the number of users, the number of votes and the number of followers of each group.
I want the query result likes this
g_id num_users unm_votes num_followers
1 2 2 3
2 3 3 2
By the way, my database is Mysql 5.0.51b.
If you want in 1 query, something like this will help you
SELECT g_id,
d1.num_users,
d2.unm_votes,
d3.num_followers
FROM groups gg
LEFT JOIN (SELECT g_id,
COUNT(u.u_id) AS num_users
FROM groups g
LEFT JOIN users u
ON u.u_groupid = g.g_id
GROUP BY g_id) d1
ON d1.g_id = gg.g_id
LEFT JOIN (SELECT g_id,
COUNT(v.v_userid) AS unm_votes
FROM groups g
LEFT JOIN votes v
ON v.v_groupid = g.g_id
GROUP BY g_id) d2
ON d2.g_id = gg.g_id
LEFT JOIN (SELECT g_id,
COUNT(f.f_userid) AS num_followers
FROM groups g
LEFT JOIN follows f
ON f.f_groupid = g.g_id
GROUP BY g_id) d3
ON d3.g_id = gg.g_id
GROUP BY gg.g_id
For the user count by group:
select g_id,count(u.uid) user_count from groups g, users u
where u.groupid = g.g_id
group by g_id
May want to read up on group by.