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;
Related
i just started my journey with SQL, and made some tables of Cyclists, and Cycling Teams.
Cyclist's table contains columns: ID, Name, Team (which is foreign key of TEAMS ID)
Team's table contains columns: ID, Name, Number of Cyclists
I want to Count number of Cyclists in each team, by using count() function ( Or basically any function, i just want to make it work )
After many minutes i figured out this query:
SELECT teams.name,
count(*) AS NumberOfCyclists FROM cyclists
JOIN teams ON cyclists.team = teams.id
group by teams.name;
and i Achieved this:
Which is all good, but when i LEFT JOIN i achieve:
My question is: How to get all of the teams (there are 15 of them, not 11), even those where the count of the cyclists is 0?
I think you misunderstand how LEFT JOIN works. The order of tables in the join is important. In a LEFT JOIN, the query returns all the rows in the left table, even if there are no matching rows in the right table. In your query, the left table is cyclists, and the right table is teams.
So your query is currently returning all cyclists, including those who have no team (the result shows that there are 3 cyclists who have no team). This is the reverse of what you want, which is all teams, even those with no cyclists.
If you want to return all the teams, then either reverse the tables in your join:
...
FROM teams
LEFT OUTER JOIN cyclists ON cyclists.team = teams.id
...
Or you could achieve the same result by using RIGHT join.
...
FROM cyclists
RIGHT OUTER JOIN teams ON cyclists.team = teams.id
...
You must count not the amount of rows (COUNT(*)) which cannot be zero but the amount of non-NULL values in definite column (the column which is used in joining condition usage is recommended) taken from right table (COUNT(table.column)). With LEFT JOIN, of course.
But the logic needs teams table to be left. And finally:
SELECT teams.name,
count(cyclists.team) AS NumberOfCyclists
FROM teams
LEFT JOIN cyclists ON cyclists.team = teams.id
group by teams.name;
Try this:
SELECT teams.name,
count(cyclists.id) AS NumberOfCyclists
FROM teams
LEFT JOIN cyclists ON cyclists.team = teams.id
group by teams.name;
The reason why this works instead of the way you have it is because it selects Teams as the base table to draw results from instead of Cyclists.
If there isn't a Cyclist record corresponding to a Team, then the Team is essentially null, and they are grouped together as such (with a null name). By going from Teams into Cyclists, you are saying to take each Team and find the Cyclist records corresponding to the Team, in which case there could be 0 or more.
As you LEFT JOIN, you get all rows from the table cyclists which can have a partner teams, when not all teams rows are NULL
So you have rows that have no oartner
I have a match table whose structure is displayed here
in this table i have column teama, teamb which are a foreign key columns referenced to team table's t_id. Basically, what i want to do is that when i select all data from this table i want it to display the values in teama, and teamb instead of their t_id. Structure of Team table is here
Query which i am writing is below:
select *
from teams,matches
where
matches.team_a=teams.t_id
and matches.team_b=teams.t_id;
You need to join 2 columns of matches to the teams table:
select
m.m_id,
t1.t_name as team_a,
t2.t_name as team_b,
m.m_time
from
matches m inner join teams as t1 on m.team_a=t1.t_id
inner join teams as t2 on m.team_b=t2.t_id
order by m.m_id;
First, never use commas in the FROM clause. Always use proper, explicit, standard JOIN syntax. You need two JOINs in fact:
select m.*, ta.t_name as name_a, tb.t_name as name_b
from matches m left join
teams ta
on m.team_a = ta.t_id left join
teams tb
on m.team_b = tb.t_id;
This uses left join just to ensure that you get all matches, even if one of the teams is missing. In this case, that is probably not an important consideration, so inner join would be equivalent.
You want two INNER JOINs from table matches to table teams, like :
SELECT
ta.t_name,
tb.t_name
FROM
matches m
INNER JOIN team as ta on ta.t_id = matches.team_a
INNER JOIN team as tb on tb.t_id = matches.team_b
You can create view after join it was make your work simple for further developement,i was improove mr.forbas code as follow
CREATE VIEW team AS select
m.m_id,
t1.t_name as team_a,
t2.t_name as team_b,
m.m_time
from
matches m inner join teams as t1 on m.team_a=t1.t_id
inner join teams as t2 on m.team_b=t2.t_id
order by m.m_id;
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
Context
I have been fiddling with a small fooseball hobby database to keep track of matches, players and goals. And came across a problem i don't quite know how to fix.
The match table has two foreign keys both pointing to tID in the team table.
The thought was that i later would be able to do a SELECT to see what teams (by name) played against eachother in a given match.
select * from `Fooseball`.`match`
INNER JOIN team T1
ON Fooseball.`match`.mHome_Team = T1.tID
INNER JOIN team T2
ON Fooseball.`match`.mAway_Team = T2.tID
WHERE mID=1
Question
1 Is their a better way to archive this, than creating two primary keys. Like, an intermediate table?
2 How can i construct my select statement so i can name the tName columns as "home" and "away" or something else? When i try and say
INNER JOIN team AS T1
Nothing changes.
Unstated additional requirements notwithstanding, this is pretty much how I would do it.
To rename columns in the result, you would do something like
SELECT m.mDate AS match_date, T1.tName AS home_team, T2.tName AS away_team
FROM Fooseball.`match` m
INNER JOIN team T1
ON m.mHome_Team = T1.tID
INNER JOIN team T2
ON m.mAway_Team = T2.tID
WHERE mID=1
For reporting, you can alias your columns with mixed case and spaces (eg. "Home Team") by enclosing the alias in double quotes.
I have two tables, one with names of teams, the other holds various selections made by users, each of which is represented by the team's team_id. I want to be able to display the actual team names for each, but can't figure out how the join would work.
Table 1
user_id(int), selection1(int), selection2(int), selection3(int)
Table 2
team_id(int), team_name(varchar)
You need three joins:
select u.user_id, t1.team_name as team_name1, t2.team_name as team_name2,
t3.team_name as team_name3
from users u left outer join
teams t1
on u.selection1 = t1.team_id left outer join
teams t2
on u.selection2 = t2.team_id left outer join
teams t3
on u.selection3 = t3.team_id;