【发布时间】:2020-05-25 11:29:53
【问题描述】:
我的数据库结构如下:
用户表:
- id
- first_name
- last_name
- fire_id (FOREIGN KEY)
匹配表:
id
winner_id (RELATIONSHIP TO "fire_id" IN USERS TABLE) string
loser_id (RELATIONSHIP TO "fire_id" IN USERS TABLE) string
pot_amount int
player1_id (RELATIONSHIP TO "fire_id" IN USERS TABLE) string
player2_id (RELATIONSHIP TO "fire_id" IN USERS TABLE) string
game_id (RELATIONSHIP TO "id" in GAMES TABLE) int
in_progress (BOOLEAN)
游戏桌:
id
name
console
我正在尝试为 PostgreSQL 构建 SQL 语句。 这就是我想要做的。 构建一个查询,获取所有 in_progress 为 false 的匹配项。也将加入用户表并包括 player1 并且如果 player2 不为 NULL,它将包括 player2 在他们的 ass 中。它还将根据比赛的“game_id”将比赛包括在查询中。
如果有人可以帮助我构建此查询,我将不胜感激。 谢谢
截至目前,我有以下查询,但它为 player1 返回一行,为 player2 返回另一行
SELECT *
FROM matches
JOIN users ON matches.player1_id = users.fire_id OR matches.player2_id = users.fire_id
LEFT JOIN games ON matches.game_id = games.id
【问题讨论】:
-
请自己做作业。开始阅读有关 sql 以及如何编写查询的信息。如果您在查询输出方面遇到任何问题,请发布查询和查询输出,我们会提供帮助。
标签: sql postgresql join select