MySql关系查询结果

时间:2016-03-07 18:32:39

标签: mysql

我创建了这个数据库。 enter image description here

我是关系数据库的新手。我正在尝试运行查询以显示

Select candidates.CandidateName, candidates.CandidateVotes, candidates.Party, race.RaceName, mainrace.MainRaceName
From candidates  
JOIN ....
...

我尝试了不同的选择,但我不断从候选人那里得到字段,但其他表格都没有。

SELECT `candidates`.`CandidateName`, `mainrace`.`MainRaceName`, `race`.`RaceName`
FROM `race`
LEFT JOIN `electionpc`.`candidates`
ON `race`.`RaceID` = `candidates`.`RaceID`

运行此查询后,我得到#1054 - Unknown column 'mainrace.MainRaceName' in 'field list'

1 个答案:

答案 0 :(得分:3)

您需要加入两个表:

SELECT candidates.CandidateName, mainrace.MainRaceName, race.RaceName
FROM candiates
JOIN race ON race.RaceID = candidates.RaceID
JOIN mainrace ON mainrace.MainID = candidates.MainID