如何修复 "missing FROM-clause entry for table" 错误

How to fix "missing FROM-clause entry for table" error

我正在尝试根据游戏 ID 获取平台名称。

我有如下三个表格,我正在尝试连接它们以获得所需的结果:

      Games

Id | .....| .....|
---|------ ------|
1  | .    | .    |
2  | .    | .    |
3  | .    | .    |
4  | .    | .    |

      Game_Platforms

Id |....|game_id| platform_id|...|
---------------------------------
1  | .  | 1     |   1        |.. |
2  | .  | 1     |   2        |.. |
3  | .  | 3     |   3        |.. |
.. | .  | 4     |   4        |.. |

    Platforms
Id| ...|...| name    |
---------------------|
1 | .  | . | iOS     |
2 | .  | . | Android |
3 | .  | . | Windows |
4 | .  | . | SteamOS |
type Platforms struct {
   Name string
}

var name []Platforms

query = postgres.Db().Select("name").
        Joins("JOIN games ON games.id = game_platforms.game_id").
        Joins("JOIN game_platforms ON game_platforms.platform_id = platforms.id").
        Where("games.id = 1").Find(&name)

我希望获得平台名称,但出现错误:

pq: missing FROM-clause entry for table "game_platforms"

我认为我写的 Joins 命令不正确,但看起来合乎逻辑,也许我错了。

您的查询缺少 FROM 子句。

我假设您想要 select 来自 platforms table 的数据。如果是,那么(根据您的代码)您应该首先加入 game_platforms 然后加入 games.

query = postgres.Db().Select("name").
    Find("platforms"). // <------ this one
    Joins("JOIN game_platforms ON game_platforms.platform_id = platforms.id").
    Joins("JOIN games ON games.id = game_platforms.game_id").
    Where("games.id = 1").Find(&name)

所以我的问题的答案并非没有@novalagung
的支持 感谢他,所以我需要将查询更改为如下所示

query = postgres.Db().Select("name").
    Joins("JOIN game_platforms ON game_platforms.platform_id = platforms.id").
    Joins("JOIN games ON games.id = game_platforms.game_id").
    Where("games.id = 1").Find(&name)

但是我有一个错误说 pq: 列引用 "name" 不明确
但这是我的错,因为我没有指定所有有问题的表,所以需要做一些小的改动来解决这个错误是 Select("platforms.name") .