简体   繁体   中英

Combine multiple tables in MySQL for View

Below is my code I am using to try and create a view from 3 tables. I am getting an error "Error Code 1052 fighterID is ambiguous". This is the first view I've ever tried to create and I can't discern what I'm doing wrong in order to populate the view correctly.

CREATE VIEW championsList AS (
SELECT champions.fighterId, fighters.fighterId, weightClasses.weightClassId, fighters.firstName, fighters.LastName, weightClasses.weightClass
FROM champions, fighters
INNER JOIN fighters champions
ON
champions.fighterId = fighters.fighterId
INNER JOIN champions weightClasses
ON
champions.weightClassId=weightClasses.weightClass



fighters
1 Frank Smith 
2 Fred  Lewis

champions
1 2
2 1

weightClasses
1 heavyweight
2 bantemweight

ChampionsList
Frank Smith bantemweight
Fred Lewis Heavyweight

Give an alias to the two columns which are both named fighterId in their respective tables:

CREATE VIEW championsList AS (
    SELECT champions.fighterId AS c_fighterId, fighters.fighterId AS f_fighterId,
        weightClasses.weightClassId, fighters.firstName, fighters.LastName,
        weightClasses.weightClass
    FROM champions INNER JOIN fighters
        ON champions.fighterId = fighters.fighterId
    INNER JOIN weightClasses
        ON champions.weightClassId = weightClasses.weightClass
)

I also cleaned up your query to what I believe you intended.

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