我正在使用它:
SELECT res.*, rac.*, u.*, t.*, c.*
FROM Results res
INNER JOIN Races rac USING (RaceID)
INNER JOIN Users u USING (UserID)
INNER JOIN Teams t USING (TeamID)
INNER JOIN Cars c USING (CarID)
WHERE res.SeasonNumber = '$SeasonNumber' AND res.LeagueID = '$LeagueID' AND Position = '1' AND ResultConfirmed = '1'
ORDER BY Position ASC
哪个工作正常,但我已经意识到我需要将CarID添加到Results表中,但是当我添加它时,它会给我一个错误,即该字段是不明确的。我想做的是从Car表中获取汽车名称,其中CarID加入汽车和结果。当我尝试这样做时:
SELECT res.*, rac.*, u.*, t.*, c.*
FROM Results res
INNER JOIN Races rac USING (RaceID)
INNER JOIN Users u USING (UserID)
INNER JOIN Teams t USING (TeamID)
INNER JOIN Cars c USING (res.CarID)
WHERE res.SeasonNumber = '$SeasonNumber' AND res.LeagueID = '$LeagueID' AND Position = '1' AND ResultConfirmed = '1'
ORDER BY Position ASC
我收到以下错误:
您的SQL语法有错误;检查手册 对应于您的MySQL服务器版本,以便使用正确的语法 靠近' .CarID)WHERE res.SeasonNumber =' 1' AND res.LeagueID =' 1'和 位置=' 1' '在第6行
答案 0 :(得分:2)
您可以将USING
子句替换为ON()
,在USING()
子句中,我猜您添加的列名与您要加入的其他表中的列名相同,但您将联接放在最后并使用别名res
mysql将不允许此
INNER JOIN Cars c ON(res.CarID =c.CarID)
如果您需要使用USING()
子句,则需要调整联接展示位置,例如
SELECT res.*, rac.*, u.*, t.*, c.*
FROM
Cars c
INNER JOIN Results res USING (CarID)
INNER JOIN Races rac USING (RaceID)
INNER JOIN Users u USING (UserID)
INNER JOIN Teams t USING (TeamID)
WHERE res.SeasonNumber = '$SeasonNumber' AND res.LeagueID = '$LeagueID' AND Position = '1' AND ResultConfirmed = '1'
ORDER BY Position ASC
但ON()
子句更具可读性