SQL查询报错:spel_plattform表FROM子句缺失及平台全名获取咨询
问题解答
1. 修复SQL查询的报错
你遇到的错误是因为查询里用到了spel_plattform表,但没把它加入查询的表列表,而且关联逻辑也不对。正确的做法是先把Spel和中间表spel_plattform通过spelid关联,再用spel_plattform里的plattformID关联Plattform表,这样才能拿到游戏和对应平台的正确数据。
修正后的完整查询(获取游戏名+完整平台名称):
SELECT Spel.spel, Plattform.Plattform AS 完整平台名称 FROM Spel JOIN spel_plattform ON Spel.SpelID = spel_plattform.spelID JOIN Plattform ON spel_plattform.plattformID = Plattform.PlattformID;
如果只需要平台ID,修正后的基础查询:
SELECT Spel.spel, spel_plattform.plattformID FROM Spel JOIN spel_plattform ON Spel.SpelID = spel_plattform.spelID;
2. 无外键时能否获取完整平台名称
完全可以。外键只是数据库用来保证数据一致性的约束(比如阻止插入不存在的平台ID),它不影响表之间的查询关联。只要两个表有共同的关联字段(比如spel_plattform的plattformID和Plattform的PlattformID),就算没有外键,也能通过关联查询拿到对应的完整平台名称。
内容的提问来源于stack exchange,提问作者medina_01
相关产品推荐
相关产品推荐

