Need some help on mysql inner join - mysql

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;

Related

Get HomeTeam ID and AwayTeam ID of each game for a specific date

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).

SQL selecting and joining two values from table into another table

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.

mySQL IF condition THEN condition

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

MYSQL, Max,Group by and Max

I am having the following two table.
1.Movie Detail (Movie-ID,Movie_Name,Rating,Votes,Year)
2.Movie Genre (Movie-ID,Genre)
I am using the following query to perform join and get the movie with highest rating in each
genre.
select Movie_Name,
max(Rating) as Rating,
Genre from movie_test
inner join movie_genre
where movie_test.Movie_ID = movie_genre.Movie_ID
group by Genre
In the output Rating and Genre are correct but the Movie_Name is incorrect.
can anyone suggest what changes I should make to get the correct movie name along with rating and genre.
SELECT g.*, d.*
FROM MovieGenre g
INNER JOIN MovieDetail d
ON g.MovieID = d.MovieID
INNER JOIN
(
SELECT a.Genre, MAX(b.Rating) maxRating
FROM MovieGenre a
INNER JOIN MovieDetail b
ON a.MovieID = b.MovieID
GROUP BY a.Genre
) sub ON g.Genre = sub.Genre AND
d.rating = sub.maxRating
There is something wrong with your schema design. If a Movie can have many Genre as well as Genre can be contain on many Movie, it should be a three table design.
MovieDetails Table
MovieID (PK)
MovieName
MovieRating
Genre Table
GenreID (PK)
GenreName
Movie_Genre Table
MovieID (FK) -- compound primary key with GenreID
GenreID (FK)
This is a common MySQL problem - specifying non-aggregate/non-aggregated-by columns in an aggregate query. Other flavours of SQL do not let you do this and will warn you.
When you do a query like yours, you are selecting non-aggregate columns in an aggregated group. Since many rows share the same genre, when you select Movie_Name it picks one row at random from each group and displays that one, because there is no general algorithm to guess the row you want and return the values of that.
You might ask 'why does it pick randomly? It could pick the one that max(Rating) belongs to?' but what about other aggregate columns, like avg(Rating)? What row does it pick there? What if two rows have the same max, anyway? Therefore it cannot have an algorithm to pick a row.
To solve a problem like this, you have to restructure your query, something like:
select Movie_Name,
Rating,
Genre from movie_test mt
inner join movie_genre
where movie_test.Movie_ID = movie_genre.Movie_ID
and Rating = (select max(Rating) from movie_test mt2 where mt.Genre = mt2.Genre
group by Genre
limit 1
This will select the row with the rating being the same as the maximum rating for that genre, using a subquery.
Query:
SELECT t.Movie_Name,
t.Rating,
g.Genre
FROM movie_test t
INNER JOIN movie_genre g ON t.Movie_ID = g.Movie_ID
WHERE t.Movie_ID = (SELECT t1.Movie_ID
FROM movie_test t1
INNER JOIN movie_genre g1 ON t1.Movie_ID = g1.Movie_ID
WHERE g1.Genre = g.Genre
ORDER BY t1.Rating DESC
LIMIT 1)

MYSQL Query multiple id to name resolution between two tables

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'