MySQL Multiple JOINS on the same cell - mysql

I'm pretty new to SQL. I've been googling for hours and I'm stuck with this problem:
I have a table games with following keys:
gameID, homeTeamID, awayTeamID
And a table teams with following keys:
teamID, teamName, teamTLC
Now I want to query a game along with the team names and team tlcs. I guess the query might look something like this:
SELECT games.id, teams.name
FROM games
WHERE games.homeTeamID = 34 OR games.awayTeamID = 34
INNER JOIN teams AS homeTeam
ON leagueGame.homeTeamID = homeTeam.teamID
INNER JOIN teams AS awayTeam
ON leagueGame.awayTeamID = awayTeam.teamID
But I'm pretty sure that this code is completely wrong. Appreciate any help.

You're not that far... :
the WHERE clause comes after the FROM ... JOIN part
as you are querying for a game, you want to filter on gameID rather than on teams
you have no table alias called teams ; you need to use the table aliases that you defined in the JOINs.
Try :
SELECT games.id, homeTeam.name AS homeTeamName, awayTeam.name AS awayTeamName
FROM games
INNER JOIN teams AS homeTeam
ON leagueGame.homeTeamID = homeTeam.teamID
INNER JOIN teams AS awayTeam
ON leagueGame.awayTeamID = awayTeam.teamID
WHERE games.gameID = ?
NB : if you need teamTLC as well, you can access it using table aliases awayTeam and homeTeam.

you need to change the order. first you need to have all your data, then your criteria.
SELECT
games.id,
teams.name
FROM
games
INNER JOIN teams as homeTeam ON leagueGame.homeTeamID = homeTeam.teamID
INNER JOIN teams as awayTeam ON leagueGame.awayTeamID = awayTeam.teamID
WHERE
games.homeTeamID = 34
OR games.awayTeamID = 34

Related

How to find the number of Distinct ID's on one table

SELECT Count(Teams.Name)
, Teams.TeamID
, Teams.Name
FROM Teams, Rosters
WHERE Rosters.TeamID = Teams.TeamID
Is my query so far. I'd like it to print the number of entries of Rosters.TeamID that correspond to Teams.TeamID.
I think the query you want is:
SELECT t.TeamID, t.Name, COUNT(*)
FROM Teams t JOIN
Rosters r
ON r.TeamID = t.TeamID
GROUP BY t.TeamID, t.Name;
Basically, your query is missing the GROUP BY, but you should also learn proper JOIN syntax.

how to join one column to all columns from another table

I have two tables, players and lineup table with all players in the team
the lineup table have this columns
`match_id`, `goalkeeper`, `center_back`, `center_back_2`, `right_outside_back`, `left_outside_back`, `defensive_center_midfielder`, `center_midfielder`, `attacking_center_midfielder`, `right_winger`, `left_winger`, `center_forward`
and all of the columns have the ID of the one player in the players table
How to join all the columns here to the player table ?
something in line with this:
select match_id,
gk.player_name as goalkeeper,
c_back.player_name as center_back
from matches
left join players as gk on gk.id = matches.goalkeeper
left join players as c_back on c_back.id = matches.center_back
...
where match_id = x
basically a lot of left_joins.
If you redesing your database model and add table that will have columns:
match_id, position_id, player_id things will be much simpler and database engine will be grateful to you.
then your SQL would look much simpler:
select positions.* from position_definition as positions
left join (select mp.*, players.player_name from match_players as mp left join players on mp.player_id = players.id where match_id = x)
as team on team.position_id = positions.id
(something like that, did not check for accuracy here) there is additional table added holding a list of all positions in a match.

sql join returns me two same values

I have two tables in mysql: matches and teams, matches have columns home_team and away_team which are connected with fk in table teams.team_id.. I want to get those names in home_team and away_team instead their id ...
I've tried this, but it returns me same value two times.. I'm doing something wrong but I can't figure it out.
Code
SELECT matches.home_team, matches.away_team, teams.name as home, teams.name as
away FROM matches left join teams ON matches.home_team = teams.team_id
left join teams as t ON matches.away_team = t.team_id
First value is correct, but second one no.
Your aliases are off. You are referring to the same first teams table twice in your current query. Try this version, which distinguishes the two joined teams table using proper aliases.
SELECT
m.home_team,
m.away_team,
t1.name AS home,
t2.name AS away
FROM matches m
LEFT JOIN teams t1
ON m.home_team = t1.team_id
LEFT JOIN teams t2
ON m.away_team = t2.team_id;

Join two columns from one table to one column from another

I am trying to use my home_team and away_team fixture_id's to get their name values from a different table.
This works to get the name value of home_team
SELECT * FROM fixtures JOIN teams ON fixtures.home_teamID = teams.TeamID
To get the name value of the away team I have the following
SELECT * FROM fixtures JOIN teams ON fixtures.home_teamID = teams.TeamID
JOIN teams ON fixtures.away_teamID = teams.TeamID
But this then returns a boolean
you should join the teams table two time using two different alias
SELECT fixtures.* a.* , b.*
FROM fixtures
JOIN teams a ON fixtures.home_teamID = a.TeamID
JOIN teams b ON fixtures.home_teamID = b.TeamID

MySQL join strange results

Can anyone help, mysql is beyond me I think!
I have the following tables:
matches
teams
competition
matches holds the details of a game between two teams and the corresponding competition.
I want to query the data base for all games/fixtures for a given competition, I want to return:
team names
team id
team competition
Here is my query:
SELECT
matches.match_id,
teamsh.team_name AS "homeTeam",
teamsa.team_name AS "awayTeam",
competition.competition_id,
competition.name
FROM
matches, teams teamsh, teams teamsa, competition
WHERE
matches.home_team_id = teamsh.team_id
AND matches.away_team_id = teamsa.team_id
AND matches.competition_id=2
For some reason this query returns all fixtures correctly for competition 2, but then it also returns rows for the fixture but as competition 1 also. I can't understand why as I have the clause:
AND matches.competition_id=2
What am I doing wrong, I have checked the database and the matches are stored correctly for each fixture.
Thanks.
You haven't explicitly linked to the competition table (so it's performing a cartesian join) - try adding:
and matches.competition_id = competition.competition_id
- to the end of your query.
Although, I would suggest rewriting the query to use the explicit join syntax - like so:
SELECT m.match_id,
h.team_name AS "homeTeam",
a.team_name AS "awayTeam",
c.competition_id,
c.name
FROM matches m
JOIN teams h ON m.home_team_id = h.team_id
JOIN teams a ON m.away_team_id = a.team_id
JOIN competition c ON m.competition_id = c.competition_id
WHERE m.competition_id=2
You forgot the JOIN condition between the competitions and matches tables.
WHERE
matches.home_team_id = teamsh.team_id
AND matches.away_team_id = teamsa.team_id
AND matches.competition_id = competition.competition_id
AND matches.competition_id=2