gpt4 book ai didi

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

转载 作者:数据小太阳 更新时间:2023-10-29 03:45:45 26 4
gpt4 key购买 nike

我正在尝试根据游戏 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 子句。

我假设您想从 platforms 表中选择数据。如果是,那么(根据您的代码)您应该首先加入 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)

关于postgresql - 如何修复 "missing FROM-clause entry for table"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57288132/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com