how to join one column to all columns from another table - mysql

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.

Related

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;

Display name of foreign key column instead of its id

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;

SQL join - one column serving as ID for two columns in another table

Okay, maybe I've absolutely goofed on the thought process behind this and I need put in my place, or maybe I'm not far off.
I have one table called TEAMS with two columns: teamID and teamName. I then have another table called WEEK12 with three columns: gameID, homeID and awayID.
I thought maybe I could use the teamID in the homeID and awayID columns for the WEEK12 table and then join that with the TEAMS table to match those two columns up with the team names. Unfortunately, I'm not having any luck. I can join and get team names to match with homeID or awayID, but I can't do both.
Any help is greatly appreciated!
SELECT w.gameID,
h.teamName AS 'Home Team',
a.teamName AS 'Away Team'
FROM WEEK12 AS w
LEFT JOIN TEAMS AS h
ON w.homeID=h.teamID
LEFT JOIN TEAMS AS a
ON w.awayID=a.teamID
You should be able to join to the same table twice in the same query. Performance hit (twice the lookups) but it should work.
SELECT home.teamName as homeTeam, away.teamName as awayTeam, week.gameID
FROM week12 week
INNER JOIN teams home ON week.homeID = home.teamID
INNER JOIN teams away ON week.awayID = away.teamID

MYSQL View and summing fields

I need some help I have been scouring the web and haven't been able to find something too similar. I have a MYSQL database for my Golf League. I need to display standings by creating a view from this database. There are 2 people per team, my primary key in 'players' is 'id' there is also a teamID (numerical value 1 - 20, 20 teams) for each player which corresponds to their teammates. Basically what I need is a view that contains 'teamID', both players 'LName' (maybe an 'LNameA','LNameB'), and a sum of the two players 'points' field. I have never summed a field from one person and another or created a view in MYSQL.
EDIT:
I was trying something like
CREATE
VIEW standings1
AS SELECT teamID, LName, points
FROM players
but need teamID to be the primaryKey of the view which will contain each players last name, and their points summed together.
Try this:
create view standings as
select teamId, group_concat(lname separator ', ') as TeamMembers,
sum(points) TotalPoints from players
group by teamId
Oh, one more thing. If you want to have the names of the players in different fields (group_concat just separate them by commas, but it is still a single field) you can use this query:
create view standings as
select a.teamId, a.lname as player1, b.lname as player2,
a.points + b.points TotalPoints
from players a
join players b ON a.teamId = b.teamId AND a.id >= b.id
group by a.teamId, a.id
having count(*) = 2
That way you can play better with the names in PHP without having to parse the ", "
If I understand your table structure, you will need a JOIN against the table's own teamID. I'm assuming the teamID refers to a team, and is not the id of the player. The trick here is to join two copies of the table on the same teamID, but where the player ids are non-equal. That should produce the pair of players per team.
CREATE VIEW standings AS
(
SELECT
p1.teamID AS teamID,
p1.id AS p1id,
p2.id AS p2id,
p1.LName AS p1LName,
p2.LName AS p2LName,
p1.score + p2.score AS totalScore
FROM
/* JOIN on matching teamID and non-matching player-id (so you don't get the same player twice) */
players p1 JOIN players p2 ON p1.teamID = p2.teamID and p1.id <> p2.id
);

mysql query table join

I have two tables, GAMES and USERS set up with the following fields:
tblGAMES-->
GAME_ID (int) primary key,
P1_ID (int),
P2_ID (int),
P3_ID (int),
P4_ID (int)
tblUSERS-->
USER_ID (int) primary key,
FIRST_NAME (text),
LAST_NAME (text)
I am querying my database to print out the details of every game row. Example:
GAME_ID:1, P1:1, P2:2, P3:3, P4:4
but instead of printing out the id which is stored in tblGAMES I would like to print out the first name of each player as it appears in the corresponding row in tblUSERS. Is this doable using a single query as opposed to multiple queries?
end result-->GAME_ID:1, P1:David, P2:Paul, P3:John, P4:Bobby
You'll need to join the users table 4 times...
select g.GAME_ID,
u1.FIRST_NAME as ulname,
u2.FIRST_NAME as u2name,
u3.FIRST_NAME as u3name,
u4.FIRST_NAME as u4name
from tblGAMES as g
left join tblUSERS as u1 on g.P1_ID =u1.USER_ID
left join tblUSERS as u2 on g.P2_ID =u2.USER_ID
left join tblUSERS as u3 on g.P3_ID =u3.USER_ID
left join tblUSERS as u4 on g.P4_ID =u4.USER_ID
(untested)
In the query you're going to want to reference tblUSERS once per JOIN. Something like this:
SELECT
tblGAMES.GAME_ID,
tblUSER1.FIRST_NAME AS P1,
tblUSER2.FIRST_NAME AS P2,
tblUSER3.FIRST_NAME AS P3,
tblUSER4.FIRST_NAME AS P4
FROM tblGAMES
INNER JOIN tblUSERS AS tblUSER1 ON tblGAMES.P1_ID = tblUSER1.USER_ID
INNER JOIN tblUSERS AS tblUSER2 ON tblGAMES.P2_ID = tblUSER2.USER_ID
INNER JOIN tblUSERS AS tblUSER3 ON tblGAMES.P3_ID = tblUSER3.USER_ID
INNER JOIN tblUSERS AS tblUSER4 ON tblGAMES.P4_ID = tblUSER4.USER_ID
Note that my syntax may need a little tweaking, I haven't written enough MySQL recently to free-hand it 100% reliably. But you get the idea. Each join gets added to the overall product independently.
You may also change the JOIN types depending on how required each player is. For example, if some games are 2-player then you'd want these to be LEFT OUTER JOINs instead so as to better handle null values, etc.