简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM