dsgwdigu84950 2019-07-31 10:00
浏览 755
已采纳

如何解决“缺少表的FROM子句条目”错误

I'm trying to get the name of platforms based on games id.

I have three tables as below and I'm trying to JOIN them to get result that needed:

      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)

I expect to get the Platforms name, but get the error:

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

I think that I wrote Joins commands incorrect, but it seems logical, maybe I'm wrong.

  • 写回答

2条回答 默认 最新

  • dtr87341 2019-07-31 10:30
    关注

    So answer to my question wasn't without of support of @novalagung
    Credit to him, so i need to change query to look like this

    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)
    

    However I had an error that said pq: column reference "name" is ambiguous
    But it is my fault because I didn't specified in question all tables, so small changes need to be done to solve this error is Select("platforms.name").

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?