krizzo
krizzo

Reputation: 1883

Select join multiple tables in mysql?

I have three tables I'm trying to select data from, each table has a pID which is what I want the join to be based on. When I run the following query I still end up with three pID fields.

What is wrong with my select join statement?

SELECT * FROM Player p
    LEFT JOIN AvgStats a ON a.pID = p.pID
    LEFT JOIN MisTotal m ON m.pID = p.pID;

Player Table
pID | Name | Age

AvgStats Table
pID | 3pt% | gamePoints

MisTotal Table
pID | Fouls | rebounds

I want to creat a table that returns

pID | Name | Age | 3pt% | gamePoints | Fouls | rebounds

Upvotes: 0

Views: 677

Answers (1)

sgeddes
sgeddes

Reputation: 62841

If I'm understanding your question correctly, just remove * from your query and specify the field(s) you want -- in this case, p.pID:

SELECT p.pId FROM Player p
    JOIN AvgStats a ON a.pID = p.pID
    JOIN MisTotal m ON m.pID = p.pID;

Given your edits, this should work:

SELECT p.pID, p.Name, p.Age, a.`3pt%`, a.gamePoints, m.fouls, m.rebounds 
...

Just make sure you include the backticks around the column with the special character.

Upvotes: 4

Related Questions