I am struggling with the WHERE part of a query. The query itself contains a LEFT JOIN based on an ID that is present in both tables. However I require the where statement to only return the largest single result that is present in one of the columns. Currently I am return all the values in the join, including values that I do not want.
My Current SQL is
SELECT u.uid, t.id
GROUP_CONCAT(u.forename, ' ', u.surname) AS name,
GROUP CONCAT(DISTINCT scores.points) AS point
FROM users AS U
JOIN teamname AS t
LEFT JOIN (
SELECT team_id, id
FROM games AS g
LEFT JOIN (
SELECT points, team_id
FROM scores as s
) AS S ON t.id = S.team_id
WHERE IF (S.points > 3, S.points > 2, S.point =1)
) AS G ON t.id = G.team_id
ORDER BY surname ASC;
The result of such might be something along the lines of
NAME | TEAM | GAMES | POINTS
Joe | 1 | 1,2,3,4 | 1,3,3,2,3
In this instance the first game was a draw and was replied resulting in a higher points score, I am only wanting the higher points score based on that game.
Any help would be appreciated.
Updated with Tables
users
uid
forename
surname
Team
id
teamname
uid
games
id
team_id
points
Still not quite sure if I understood your tables correctly. It seems a users has one or more teams, each team has one or more games with one or more results per game. You want to show for each user and each team the games concatenated in one column and the highest points for each game concatenated in a second column.
If my assumptions are correct the following query should do the trick. Basically, you first group the data by user/team/game and select the max points per game, then you group the results by user/team and concatenate the games and points.
Please let me know if I misunderstood any of your requirements.
Example in an SQL Fiddle
SELECT
t.uid,
t.forename,
t.team_id,
GROUP_CONCAT(t.game_id) as games,
GROUP_CONCAT(t.max_points) as max_points
FROM (
SELECT
users.uid,
users.forename,
teams.id AS team_id,
games.id AS game_id,
max(games.points) as max_points
FROM
users
LEFT JOIN teams ON users.uid = teams.uid
LEFT JOIN games ON teams.id = games.team_id
GROUP BY
users.uid,
users.forename,
teams.id,
games.id
) t
GROUP BY
t.uid,
t.forename,
t.team_id
Related
my heads hurt from trying to figure this out!
I have three tables
-Players
-Teams
-Games
Three junction tables with two columns.
-player_teams
-teams_games
-player_games
I need to list all Players who are in a Game (eg: Game_id = 111 from variable) that are not assigned a Team(in this game). i call them orphaned players
Basically get Teams that are in the game, get their Players and reverse match against Games_players. or the other way round i suppose.
i tried for two day no luck!
thanks!
/J
p.s after i posted this i got this far but it seems to complex!
SELECT * from players
JOIN
(SELECT DISTINCT games_players.player_id from games_players
Left JOIN
(Select team_players.player_id p1 from team_players
inner join (Select * from games_teams where games_teams.game_id = :P1) AS tm1 ON team_players.team_id = tm1.team_id) As f1
On games_players.player_id = f1.p1
where p1 is null) as q1
on players.player_id = q1.player_id
As the game is given, you can just look at the players in the game and the players of the teams in the game:
select *
from players
where player_id in
(
select player_id
from player_games
where game_id = 111
)
and player_id not in
(
select pt.player_id
from player_teams pt
join teams_games tg using (team_id)
where tg.game_id = 111
);
I am using MySQL 5.6.
I have a SQL table with a list of users:
id name
1 Alice
2 Bob
3 John
and a SQL table with the list of gifts for each user (numbered in order of preference):
id gift rank
1 balloon 2
1 shoes 1
1 seeds 3
1 video-game 1
2 computer 2
3 shoes 2
3 hat 1
And I would like a list of the preferred gift for each user (the highest rank - if two gifts have the same rank, pick only one randomly) (bonus: if the list could be randomized, that would be perfect!):
id name gift rank
2 Bob computer 2
1 Alice shoes 1
3 John hat 1
I tried to use the clause GROUP BY but without any success.
Considering rank as a part of your data; Without using window functions or complex sub queries
SELECT u.id, u.name, g.gift
FROM users u
JOIN gifts g ON g.id = u.id
LEFT JOIN gifts g2 ON g2.id = g.id AND g2.rank > g.rank
WHERE g2.id IS NULL;
Added link http://sqlfiddle.com/#!9/62f59e/15/0
You can use row_number to get one row for each User.(Mysql 8.0+)
SELECT A.ID,NAME,GIFT,`RANK` FROM USERS A
LEFT JOIN (
SELECT ID,GIFT,`RANK` FROM
(SELECT *,ROW_NUMBER() OVER(PARTITION BY ID ORDER BY `RANK` ASC) AS RN FROM X) X
WHERE RN =1
) B
ON A.ID= B.ID
I do not know DB what you use. And I'm not an expert in SQL(I can have some mistake in next). But I think it is not difficult.
So I can give you just advice that you have to think gradually. Let me write.
First All I need is the highest rank. So I have to get this.
SELECT MAX(RANK)
FROM GIFT
GROUP BY ID
And then I think that I need get gifts from this rank.
SELECT GIFT.*
FROM GIFT
INNER JOIN(
SELECT ID, MAX(RANK)
FROM GIFT
GROUP BY ID
) filter ON GIFT.ID = filter.ID AND GIFT.RANK = filter.RANK
I think this is the table what you want!
So If below code works, That's what you really want.
SELECT *
FROM USER
LEFT OUTER JOIN(
above table
) GIFT ON USER.ID = GIFT.ID
But Remember this, I said I'm not an expert in SQL. There can be better way.
Checkout the query
SELECT tbluser.id,name,gift,rank into tblrslt
FROM tbluser
LEFT JOIN tblgifts
ON tbluser.id = tblgifts.id order by id,rank;
SELECT tt.*
FROM tblrslt tt
INNER JOIN
(SELECT id, min(rank) AS rank
FROM tblrslt
GROUP BY id) groupedtt
ON tt.id = groupedtt.id
AND tt.rank = groupedtt.rank order by id
In MySQL versions older than 8 you have no ranking functions available. You'll select the minimum rank per user instead and use these ranks to select the gift rows. This means you access the gifts table twice.
I suggest this:
select *
fron users u
join gifts g
on g.id = u.id
and (g.id, g.rank) in (select id, min(rank) from gifts group by id)
order by u.id;
If you also want to show users without gifts, simply change the inner join to a left outer join.
I have 2 tables:
Table 1 called teams: that contains different columns like Team_ID, Team_Name... etc.
table 2 called matches: that contains different columns like Match_ID, Home, Away, Match_Date.
Home and away are the names of 2 teams that I generated,
I tried different queries to generate a result where I have teams ID instead of teams names
for example:
SELECT t1.Home,t2.Away from
(SELECT a.Team_ID AS Home, b.Match_Date from teams a
INNER JOIN matches b ON a.Team_Name=b.Home
Where b.Match_Date="2020-05-29 23:59:59") t1,
(SELECT a.Team_ID AS Away, b.Match_Date from teams a
INNER JOIN matches b on a.Team_Name=b.Away
Where b.Match_Date="2020-05-29 23:59:59") t2;
But it didn't give me the result that I'm looking for
and After getting the result, I just to want filter it using a specific date like WHERE Match_Date= "date"
An image to clarify what I'm trying to do:
You want two joins on teams (one for the home team and the other for the away team), and a where clause to filter on the match date:
select th.team_id home, ta.team_id away, m.match_date
from matches m
inner join teams th on th.team_name = m.home
inner join teams ta on ta.team_name = m.away
where m.match_date = 'date1'
Note that you should not be storing the team name in matches, but the team id instead (which, presumably, is the primary key of teams).
I have 3 tables
draws users and ratings
users contains the user_id, draws contains an id for the draw and the user_id who is the owner of that draw, in the ratings table i save the user that gives the rating, the draw_id and the score given. Now i need to list to the user his votes received, so i need to check in the ratings table which users has voted on his draws, but i don't know how because the queries i've tried lists me only 1 record and i need all records.
Something like this:
5 stars | Draw Name | User
4 stars | Draw Name | User
This is th query i have until now, i achieved to show the most voted users but not this one.
SELECT u.usuario, u.id_usuario, d.id, COUNT(v.id) AS votos
FROM valoraciones v
JOIN dibujos d ON v.id_dibujo = d.id
JOIN usuarios u ON v.id_quien = u.id_usuario GROUP BY v.fecha ORDER BY votos DESC
This is driving me crazy.
Appreciate any help
Ok, some people here thinks is funny to downvote a post, well i'm posting the solution cause i know that it can be helpfull to.
This is the Query i built:
SELECT u.usuario, u.id_usuario, d.id, v.valoracion, v.fecha
FROM icar_valoraciones v
JOIN icar_dibujos d ON v.id_dibujo = d.id
JOIN icar_usuarios u ON v.id_quien = u.id_usuario WHERE d.id_quien = '{ID OF THE USER LOGGED IN}' GROUP BY v.id ORDER BY v.fecha DESC
You can find it here => http://sqlfiddle.com/#!9/b44fe/2
I have 3 tables
user
id | etc |
game
id | etc
user_game
user_id | game_id
I want to create a situation where n users couldn't create another game if and only if they already have a game between those n users.
so for example, assume we have user_ids: 1,2 and they're both playing in game_id 1, they can't create another game between them, they could however create a new game if user_id 3 joins the game.
Is it possible to query for such thing, if so - how could I go about it? tried playing with queries, joins, selects and where's for far too long :)
EDIT: the end result should be that I query for severel users, and see if they're elgible to play together (ie. no records were found of those n users playing together).
Assume that you have a table with the list of proposed users. I'll call it ProposedUsers. You can do the following to find any game_id that has those users:
select game_id
from user_game ug join
ProposedUsers pu
on ug.user_id = pu.user_id
group by game_id
having count(distinct ug.user_id) = (select count(distinct user_id) from ProposedUsers)
Your question is a bit unclear as to whether the match to an existing game is exact (no other users on the game) or not. The above handles the subset case. The exact match would be:
select game_id
from user_game ug left outer join
ProposedUsers pu
on ug.user_id = pu.user_id
group by game_id
having count(distinct ug.user_id) = (select count(distinct user_id) from ProposedUsers)