I have 2 tables like this:
[games]
gameid
hometeamid
awayteamid
score
and
[teams]
teamid
teamname
How would i create a query to output something like:
[home team][away team][score]
49ers chargers 28-17
You see, i need to resolve 2 team names with 2 team ids within the same table and output just the names. Thanks in advance for your help!
SELECT
ht.TeamName AS HomeTeam,
vt.TeamName AS AwayTeam,
g.Score
FROM
games g INNER JOIN teams ht
on g.hometeamid = ht.teamid
INNER JOIN teams vt
on g.awayteamid = vt.teamid
I'd suggest naming the tables "game" and "team" - as I'm not a fan of plural table names. I'm not alone in this opinion, but it's really a style/preference thing.
This should work for you:
select t1.teamname, t2.teamname, g.score
from games as g
left outer join team as home_team
on g.hometeamid = home_team.id
left outer join team as away_team
on g.awayteamid = away_team.id
SELECT (SELECT teams.teamname FROM teams WHERE teams.teamid=games.awayteamid) AS awayteam,
(SELECT teams.teamname FROM teams WHERE teams.teamid=games.hometeamId) AS hometeam, score
FROM games
WHERE gameid = '1'
Related
I have two tables, named matches and teams.
Matches:
tournament_id, match_id, score, home_team_id and away_team_id
enter image description here
Teams: team_id, team_name
enter image description here
My SQL query must result in the filled in team_name for the home_team_id as also the away_team_id
I know that I will need INNER JOIN to get the result, but the only good result I get is the home_team_id withe the team_name or the away_team_id with the team_name but not both. See code below
SELECT matches.tournament_id,
matches.match_id,
matches.score,
matches.home_team_id,
matches.away_team_id,
teams.team_id,
teams.team_name
FROM matches
INNER JOIN teams ON matches.home_team_id = teams.team_id
WHERE matches.tournament_id = 'WC-1930'
How can I get the result of both the team_name for the home_team_id and the away_team_id?
Does this help achieve what you want- adding a second link to teams based on the away team. EDIT: It was pointed out that my change ended up with some duplicate field names so have fixed this as well.
SELECT matches.tournament_id,
matches.match_id,
matches.score,
matches.home_team_id,
matches.away_team_id,
h.team_id as home_id,
h.team_name as home_name,
a.team_id as away_id,
a.team_name as away_name
FROM matches
INNER JOIN teams h ON matches.home_team_id = h.team_id
INNER JOIN teams a ON matches.away_team_id = a.team_id
WHERE matches.tournament_id = 'WC-1930'
ORDER BY matches.match_id ASC;
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 want to find for each
genre of movie, find the N actors who have played in most movies
of the genre
I have done this:
select genre.genre_name,actor.actor_id,count(genre.genre_name) from genre
inner join movie_has_genre on movie_has_genre.genre_id=genre.genre_id
inner join movie on movie_has_genre.movie_id=movie.movie_id
inner join role on movie.movie_id=role.movie_id
inner join actor on actor.actor_id=role.actor_id
group by genre.genre_name,actor.actor_id;
which gives as a result for each genre how many movies of that genre every actor has played and now i want to find for each genre the actor that has played the most moviesof that genre.
Tables and their columns:
actor(actor_id,name)
role(actor_id,movie_id)
movie(movie_id,title)
movie_has_genre(movie_id,genre_id)
genre(genre_id,genre_name)
Also the result should be something like this:
Action 22591 7
Horror 25863 3
Horror 24867 3
Comedy 23476 2
Drama 14536 1
Drama 19634 1
Drama 17563 1
Man, what I'd do is the next (supposing your code is working well):
-- Notice this is your code with some aliases, nothing else.
-- Just for making mi job easier.
create view frequency as
select genre.genre_name as genre_ name,
actor.actor_id as actor_id,
count(genre.genre_name) as freq
from genre
inner join movie_has_genre on movie_has_genre.genre_id=genre.genre_id
inner join movie on movie_has_genre.movie_id=movie.movie_id
inner join role on movie.movie_id=role.movie_id
inner join actor on actor.actor_id=role.actor_id
group by genre.genre_name,actor.actor_id;
-- And this is my proposal
-- Take the max frequency per each category
-- and find the guy who possesses it (maybe 2 or more...)
select genre.genre_name,actor.actor_id
from frequency as tbl1 inner join
(
-- The max frequency in a genre.
select f.genre_name,
max(f.freq) as max_freq
from frequency f
group by(genre_name)
) as tbl2 on (tbl1.genre_name = tbl2.genre_name)
where tbl1.freq = tbl2.max_freq;
And well, there's one problem: It may return more than one actor per category, if there's a tie. But how can I know who is the winner? I let it for you. Maybe it's wrong, I don't think so, but we're both learning! Hope I'd help you.
You need to use the MAX() function. Some SQL implementations (such as Oracle) allow you to do this: SELECT MAX(COUNT(whatever)) but MySQL isn't one of them.
One way to do what you want is this:
select genre_name, actor_id, max(genrecount)
from (
select genre.genre_name, actor.actor_id, count(genre.genre_name) as genrecount
from genre
inner join movie_has_genre on movie_has_genre.genre_id=genre.genre_id
inner join movie on movie_has_genre.movie_id=movie.movie_id
inner join role on movie.movie_id=role.movie_id
group by genre.genre_name,actor.actor_id
) as topactor
This does the outer SELECT on the table derived from the inner SELECT.
I have two tables, "teams" and "matches". I want to select and replace two team ID's from matches table with actual team names. I am able to get only one team name and I am not sure how I could select two.
SELECT m.*, t.teamId, t.teamName FROM matches AS m
JOIN teams AS t ON m.homeTeam = t.teamId
Matches Table
ID*
Date
homeTeam (id)
awayTeam (id)
Teams Table
ID*
Name
You need to join your teams table twice. Once for each team entry in your matches table. It should look something like this
SELECT
m.id
, m.date
, h.teamId as homeTeamID
, h.teamName as homeTeamName
, a.teamId as awayTeamID
, a.teamName as awayTeamName
FROM
matches AS m
JOIN teams AS h -- home team
ON m.homeTeam = h.teamId
JOIN teams AS a -- away team
on m.awayTeam = a.teamId
I've had to guess what you named the fields, but this should be enough to get you started.
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