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

如何解决“缺少表的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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?