登录
首页 >  Golang >  Go问答

如何修复“缺少表的 FROM 子句条目”错误

来源:stackoverflow

时间:2024-04-17 22:45:35 218浏览 收藏

来到golang学习网的大家,相信都是编程学习爱好者,希望在这里学习Golang相关编程知识。下面本篇文章就来带大家聊聊《如何修复“缺少表的 FROM 子句条目”错误》,介绍一下,希望对大家的知识积累有所帮助,助力实战开发!

问题内容

我正在尝试根据游戏 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:缺少表“game_platforms”的 from 子句条目

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


解决方案


所以我的问题的答案并非没有@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:列引用“名称”不明确
但这是我的错,因为我没有指定有问题的所有表,因此需要进行一些小的更改来解决此错误,即 select("platforms.name")。

您的查询缺少 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)

终于介绍完啦!小伙伴们,这篇关于《如何修复“缺少表的 FROM 子句条目”错误》的介绍应该让你收获多多了吧!欢迎大家收藏或分享给更多需要学习的朋友吧~golang学习网公众号也会发布Golang相关知识,快来关注吧!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>