Joining separate columns from one table in SELECT statement
I have been struggling with this issue for 3 hours. I know I can work around things by putting the data in two different tables but it defeats the object as the data is intrinsically linked and I know the answer will be simple but have tried all ways to get it to work following numerous links/examples but find it very difficult to understand how the example relates to my issue.
This is not the actual SQL but will illustrate exactly what I am trying to do It is a mySQL database.
playerName = Johan Djourou
nationality = Côte d'Ivoire
playsFor = Switzerland
tbl_Soccer_Player
playerID,
playerName,
nationalityID,
playsForID,
CONSTRAINT fk1 FOREIGN KEY (nationalityID) REFERENCES tbl_nationality(notionalityID),
CONSTRAINT fk2 FOREIGN KEY (playsForID) references tbl_nationality(notionalityID)
tbl_nationality
nationalityID
nationality // french, spanish, belgian
country // france, spain, belgium
SELECT playerName, nationality, country FROM tbl_Soccer_Player
JOIN tbl_nationality ON tbl_Soccer_Player.nationalityID = tbl_nationality.nationalityID
... now how do I join the county when they are from the same table???
Many Thanks in Advance, Mark
PS. I used to solve this sort of thing by using the Microsoft Access GUI query builder to put together query's but work on Linux Ubuntu now. I will look for a linux based app to do this but 开发者_如何学编程does anybody know a good one to use?
You make another join with the same table, and use aliases to tell them apart:
SELECT playerName, n.nationality, p.country
FROM tbl_Soccer_Player
JOIN tbl_nationality as n ON tbl_Soccer_Player.nationalityID = n.nationalityID
JOIN tbl_nationality as p ON tbl_Soccer_Player.playsForID = p.nationalityID
精彩评论