0

我有两张表,我想从中获取信息。第一个表的名称是 Playing,另一个是 Game。

Player Game_ID Points

Game_ID Length Judged_by

我想编写一个只显示具有相同游戏 ID 的玩家的 SQL 查询。还有那场比赛的长度。我对 SQL 很陌生,不知道该怎么做。

4

1 回答 1

4

不太确定你想要达到什么目标。如果您希望所有玩过 1 个以上游戏的用户使用:

select p.player, g.game_id, g.length 
from game g
inner join playing p on g.game_id = p.game_id
group by game_id
having count(game_id) > 1

如果您想要所有玩过特定游戏的玩家,请使用(game_id 是一个示例):

select p.player, g.game_id, g.length 
from game g
inner join playing p on g.game_id = p.game_id
where game_id = 123

编辑

干得好:

select p.player, g.game_id, g.length
from game g 
inner join playing p on g.game_id = p.game_id
where g.game_id in (  select g.game_id
                      from game g
                      inner join playing p on g.game_id = p.game_id
                      group by g.game_id
                      having count(p.game_id) > 1
                    )
order by g.game_id

请查看此SQLFiddle 示例以验证它是否正常工作。

编辑 2

我不知道playingplayers表之间的关系。我使用了列idplayer在这个例子中也得到了玩家的名字:

select g.game_id, g.length, p.first_name, p.last_name
from game g 
inner join playing pg on g.game_id = pg.game_id
inner join Players p on p.id = pg.player
where g.game_id in (  select g.game_id
                      from game g
                      inner join playing pg on g.game_id = pg.game_id
                      group by g.game_id
                      having count(pg.game_id) > 1
                    )
order by g.game_id

最后编辑:

select g.game_id, g.length, p.first_name, p.last_name, j.Judge_firstname, j.Judge_lastname
from game g 
inner join playing pg on g.game_id = pg.game_id
inner join Players p on p.id = pg.player
inner join judges j on j.judge_id = g.judged_by
where g.game_id in (  select g.game_id
                      from game g
                      inner join playing pg on g.game_id = pg.game_id
                      where play_date > to_date('2012-05-01', 'YYYY-MM-DD')
                      group by g.game_id
                      having count(pg.game_id) > 1
                    )
order by g.game_id

这个例子

于 2012-05-18T20:48:26.460 回答