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

如何解决“缺少表的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 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作