我有一个数据库,可记录7场橄榄球比赛的球队和比分。 球队每周都会改变,但每次球员进入一支获胜球队时,他们都会获得3分(1分为平局0表示亏损)。
我希望能够选择身份证号为21的玩家,制作他参加过的游戏列表,然后与其他玩家一起玩总点数和游戏。
我一直在创建查询,以便在过去几个小时内对此进行评估,但似乎无法破解它。我的流程如下:
1)列出玩家21玩过的所有游戏
2)列出在玩家21玩过的游戏中玩过的所有玩家ID。
3)对于上面列出的每个玩家ID,当他们出现在玩家21玩过的任何游戏中时,请将列出的玩家ID的总分数计算在内,并计算他们出现的次数。
1)列出玩家21玩过的所有游戏
mysql> SELECT Game, GameDate, Team, PlayerID FROM Teams WHERE PlayerID='21';
+------+------------+------+----------+
| Game | GameDate | Team | PlayerID |
+------+------------+------+----------+
| A | 2017-01-10 | A | 21 |
| A | 2017-01-17 | A | 21 |
| A | 2017-01-24 | A | 21 |
| B | 2017-01-10 | A | 21 |
| B | 2017-01-17 | A | 21 |
| B | 2017-01-24 | A | 21 |
| C | 2017-01-10 | A | 21 |
| C | 2017-01-17 | A | 21 |
+------+------------+------+----------+
8 rows in set (0.00 sec)
2)列出在玩家21玩过的游戏中玩过的所有玩家ID。
如果我尝试使用LIMIT 1 OFFSET 0
运行此查询,则会出现more than one row selected
错误。
mysql>
SELECT PlayerID, Game, GameDate, Team
FROM Teams
WHERE Game=(SELECT Game FROM Teams WHERE PlayerID='21' LIMIT 1 OFFSET 0)
AND GameDate=(SELECT GameDate FROM Teams WHERE PlayerID='21' LIMIT 1 OFFSET 0)
AND Team=(SELECT Team FROM Teams WHERE PlayerID='21' LIMIT 1 OFFSET 0);
+----------+------+------------+------+
| PlayerID | Game | GameDate | Team |
+----------+------+------------+------+
| 5 | A | 2017-01-10 | A |
| 8 | A | 2017-01-10 | A |
| 11 | A | 2017-01-10 | A |
| 12 | A | 2017-01-10 | A |
| 14 | A | 2017-01-10 | A |
| 15 | A | 2017-01-10 | A |
| 21 | A | 2017-01-10 | A |
+----------+------+------------+------+
7 rows in set (0.00 sec)
这列出了在第一场比赛/第一场比赛中所有参赛者,其中包括玩家21。
是否有某种形式的loop
我可以将此查询加入OFFSET @X
以便我可以将所有这些游戏和玩家ID列在一个表中列出?
这就是我的尝试:
SET @GameNumber = (SELECT Count(*) FROM Teams WHERE PlayerID='21');
SET @GameCount = 0;
SELECT PlayerID, Game, GameDate, Team
FROM Teams
WHILE(@GameNumber < @GameCount) DO
WHERE Game=(SELECT Game FROM Teams WHERE PlayerID='21' LIMIT 1 OFFSET @GameNumber)
AND GameDate=(SELECT GameDate FROM Teams WHERE PlayerID='21' LIMIT 1 OFFSET @GameNumber)
AND Team=(SELECT Team FROM Teams WHERE PlayerID='21' LIMIT 1 OFFSET @GameNumber);
SET @GameNumber = @GameNumer+1;
END WHILE;
编辑1
我已经达到了这样的程度,我至少可以玩其他玩家玩过21的游戏。
SELECT Players.PlayerID, COUNT(*) AS Games
FROM Teams, Players
WHERE
Teams.PlayerID=Players.PlayerID
AND
(
(
Game=(SELECT Game FROM Teams WHERE PlayerID='21' LIMIT 1 OFFSET 0)
AND GameDate=(SELECT GameDate FROM Teams WHERE PlayerID='21' LIMIT 1 OFFSET 0)
AND Team=(SELECT Team FROM Teams WHERE PlayerID='21' LIMIT 1 OFFSET 0)
)
or
(
Game=(SELECT Game FROM Teams WHERE PlayerID='21' LIMIT 1 OFFSET 1)
AND GameDate=(SELECT GameDate FROM Teams WHERE PlayerID='21' LIMIT 1 OFFSET 1)
AND Team=(SELECT Team FROM Teams WHERE PlayerID='21' LIMIT 1 OFFSET 1)
)
or
(
Game=(SELECT Game FROM Teams WHERE PlayerID='21' LIMIT 1 OFFSET 2)
AND GameDate=(SELECT GameDate FROM Teams WHERE PlayerID='21' LIMIT 1 OFFSET 2)
AND Team=(SELECT Team FROM Teams WHERE PlayerID='21' LIMIT 1 OFFSET 2)
)
or
(
Game=(SELECT Game FROM Teams WHERE PlayerID='21' LIMIT 1 OFFSET 3)
AND GameDate=(SELECT GameDate FROM Teams WHERE PlayerID='21' LIMIT 1 OFFSET 3)
AND Team=(SELECT Team FROM Teams WHERE PlayerID='21' LIMIT 1 OFFSET 3)
)
or
(
Game=(SELECT Game FROM Teams WHERE PlayerID='21' LIMIT 1 OFFSET 4)
AND GameDate=(SELECT GameDate FROM Teams WHERE PlayerID='21' LIMIT 1 OFFSET 4)
AND Team=(SELECT Team FROM Teams WHERE PlayerID='21' LIMIT 1 OFFSET 4)
)
or
(
Game=(SELECT Game FROM Teams WHERE PlayerID='21' LIMIT 1 OFFSET 5)
AND GameDate=(SELECT GameDate FROM Teams WHERE PlayerID='21' LIMIT 1 OFFSET 5)
AND Team=(SELECT Team FROM Teams WHERE PlayerID='21' LIMIT 1 OFFSET 5)
)
or
(
Game=(SELECT Game FROM Teams WHERE PlayerID='21' LIMIT 1 OFFSET 6)
AND GameDate=(SELECT GameDate FROM Teams WHERE PlayerID='21' LIMIT 1 OFFSET 6)
AND Team=(SELECT Team FROM Teams WHERE PlayerID='21' LIMIT 1 OFFSET 6)
)
or
(
Game=(SELECT Game FROM Teams WHERE PlayerID='21' LIMIT 1 OFFSET 7)
AND GameDate=(SELECT GameDate FROM Teams WHERE PlayerID='21' LIMIT 1 OFFSET 7)
AND Team=(SELECT Team FROM Teams WHERE PlayerID='21' LIMIT 1 OFFSET 7)
)
)
GROUP BY Teams.PlayerID
ORDER BY Games DESC
;
以上为您提供此表:
+----------+-------+
| PlayerID | Games |
+----------+-------+
| 15 | 8 |
| 21 | 8 |
| 8 | 8 |
| 14 | 6 |
| 5 | 6 |
| 19 | 5 |
| 11 | 5 |
| 10 | 3 |
| 12 | 3 |
| 4 | 2 |
+----------+-------+
10 rows in set (0.01 sec)
答案 0 :(得分:0)
Lads,谈论成为我的初学者。我只是在查看subquery returns more than 1 row
错误,我遇到了ANY
。一个3个字母的单词,让我的生活更轻松。
SET @IDNumber := 21;
SELECT Players.PlayerID, Players.Name, COUNT(Games.Points) AS Games_Played, SUM(Games.Points) AS Points, SUM(Points/Games_Played) AS Points_Per_Game
FROM Teams, Players, Games
WHERE
Teams.PlayerID=Players.PlayerID
AND
Games.Game=Teams.Game
AND
Games.Team=Teams.Team
AND
Games.GameDate=Teams.GameDate
AND
(
CONCAT(Games.Game, '_', Games.Team, '_', Games.GameDate)= ANY(SELECT CONCAT(Teams.Game, '_', Teams.Team, '_', Teams.GameDate) FROM Teams WHERE PlayerID=@IDNumber)
)
GROUP BY Teams.PlayerID
ORDER By Points_per_Game DESC, Points DESC, Games_Played
;