帮助连接多个表的 SQL 查询
首先我将解释这个情况,我有一个具有这样结构的表tbl_game
。该表包含游戏开始的时间和配对玩游戏的时间
| id | time | pair_id |
-----------+--------------+ ---------------
1 | 123123123 | 1 |
2 | 123168877 | 1 |
,我还有另一个表 tbl_throws 保存每个玩家的得分。如果您想知道,这是一个基本的掷骰子游戏
| id | game_id | player_id | score |
-----------+--------------+---------------+---------|
| 1 | 1 | 1 | 2 |
| 2 | 1 | 2 | 5 |
| 3 | 1 | 1 | 9 |
| 4 | 1 | 2 | 11 |
| 5 | 2 | 1 | 7 |
| 6 | 2 | 2 | 6 |
现在,id
这里是投掷 ID,而不是游戏 ID。这里,player_id
1 和 2 的每个玩家都掷了两次骰子,并获得了各自的得分,所有这些得分都在同一场比赛中显示,而在另一场比赛中只出现一次
现在,使用这两个表,我需要创建一条记录设置,每个玩家在一场比赛中的总得分
| game_id | game_time | player1_total | player2_total|
|------------+-----------+---------------+--------------|
| 1 | 123123123 | 11 | 16 |
| 2 | 123168877 | 7 | 6 |
我尝试了很多胡言乱语的查询,但没有给出正确的结果? 对此的正确查询是什么?
更新
因为,大多数答案都受到以下事实的限制:player1id 和player2id 必须已知或固定。
因此,我即将提供的信息可能有助于消除困惑。 还有另外一个表,保存着玩家的信息。 tbl_pupil
结构如下所示
| id | unique_id | name |
|---------+---------------+----------|
| 1 | 001 | some |
| 2 | 002 | another |
,这些播放器统称为另一个表中的一对 tbl_pair
| id | player1 | player2 |
|---------+---------------+----------|
| 1 | 1 | 2 |
So, now
select
g.id
g.time
p1.id as player1id
p1.name as player1name
t.score as player1score
p2.id as player2id
p2.name as player2name
t.score as player2score
FROM
tbl_game g,
inner join tbl_pair as pair on g.pair_id = pair.id
inner join tbl_pupil as p1 on p1.id = pair.player1
inner join tbl_pupil as p2 on p2.id = pair.player2
inner join tbl_throw as t on g.id = t.game_id
这是我的初步查询,它以这样的方式带来记录集
| id | time | player1id | player1name | player1score | player2id | player2name | player2score |
----------------------------------------------------------------------------------------------------------------------------
| 1 | 12 | 1 | some | 5 | 2 | another | 2 |
| 1 | 12 | 1 | some | 5 | 2 | another | 5 |
| 1 | 12 | 1 | some | 9 | 2 | another | 9 |
| 1 | 12 | 1 | some | 11 | 2 | another | 11 |
现在我只是显示结果顺便说一下,一个游戏ID。我没有保存足够的知识,无法将上述记录分组为一个,其中玩家1单独的得分总和在一列中,玩家2单独的得分总和在另一列中。
First I will explain the case, I have a table tbl_game
with a structure as such. This table contains, the time where the game was started and pair playing the game
| id | time | pair_id |
-----------+--------------+ ---------------
1 | 123123123 | 1 |
2 | 123168877 | 1 |
and I have another table tbl_throws
which holds the score for each player. In case you are wondering, this a basic dice rolling game
| id | game_id | player_id | score |
-----------+--------------+---------------+---------|
| 1 | 1 | 1 | 2 |
| 2 | 1 | 2 | 5 |
| 3 | 1 | 1 | 9 |
| 4 | 1 | 2 | 11 |
| 5 | 2 | 1 | 7 |
| 6 | 2 | 2 | 6 |
Now, id
here is the throw id, not the game id. Here each player with player_id
1 and 2 has throws the dice twice and got the respective score as presented all in same game and just one time in another
Now, using these two table, I need to create a record set, that the total score of each player in one game
| game_id | game_time | player1_total | player2_total|
|------------+-----------+---------------+--------------|
| 1 | 123123123 | 11 | 16 |
| 2 | 123168877 | 7 | 6 |
I tried lots of mumbo jumbo queries, but nothing is giving corrent result?
What is the correct query for this?
Update
Since, most of the answers were bounded by a fact that, player1id and player2id had to be known or fixed.
So may be the information I am about to provide will help to clear the confusion.
there is another table, which holds the information of the player. tbl_pupil
Structure is like the following
| id | unique_id | name |
|---------+---------------+----------|
| 1 | 001 | some |
| 2 | 002 | another |
and these player are collectively called, a pair in another table tbl_pair
| id | player1 | player2 |
|---------+---------------+----------|
| 1 | 1 | 2 |
So, now
select
g.id
g.time
p1.id as player1id
p1.name as player1name
t.score as player1score
p2.id as player2id
p2.name as player2name
t.score as player2score
FROM
tbl_game g,
inner join tbl_pair as pair on g.pair_id = pair.id
inner join tbl_pupil as p1 on p1.id = pair.player1
inner join tbl_pupil as p2 on p2.id = pair.player2
inner join tbl_throw as t on g.id = t.game_id
This is my preliminary query, which brings the record set, on a way as such
| id | time | player1id | player1name | player1score | player2id | player2name | player2score |
----------------------------------------------------------------------------------------------------------------------------
| 1 | 12 | 1 | some | 5 | 2 | another | 2 |
| 1 | 12 | 1 | some | 5 | 2 | another | 5 |
| 1 | 12 | 1 | some | 9 | 2 | another | 9 |
| 1 | 12 | 1 | some | 11 | 2 | another | 11 |
Now I am just showing the results of one game id by the way. I don't save sufficient knowledge, to group the above record into one, with player1 separate sum score in one column and playe2's separate sum of score in another column.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
试试这个:
Try this:
这与其他一些答案类似,但关键不依赖于玩家 ID 为 1 和 2。
只是为了好玩,我概括了上述内容以允许更多 > > 2名玩家:
2个CTE表仅显示我正在使用的测试数据
This is similar to some of the other answers, but crucially doesn't depend on the player IDs being 1 and 2.
Just for fun I've generalized the above out to allow more > 2 players:
The 2 CTE tables just show the test data I'm using
您需要内部连接两个表,并汇总您的分数。要执行基本的枢轴,您需要在我使用
CASE
语句按玩家进行聚合之后进行。You need to inner join the two tables, and aggregate your scores. To do the basic pivot you are after I used a
CASE
statement to aggregate by player.我认为这应该可以完成你想做的事情。
I think this should do pretty much what you want to do.