我正在为我的Magic:The Gathering Playgroup开发一个数据库,该数据库可以跟踪甲板,更具体地说,哪些甲板可以胜过其他人等等。
表“Wins”如下所示:
表格玩家很容易:
现在我想创建一个查询,提供一个预期Winrate表(在4人游戏中为1/4,在5人游戏中为1/5等)以及每个玩家的实际胜利数量(以及后来的预期和实际的Winrate,但我想,一旦我让这个孩子上班,我可以独自完成工作)。 到目前为止,我已经想到了这样的事情:
SELECT a.'Player',a.'ExpectedWinrate',b.'Wins'
FROM(
SELECT
ROUND(((SUM(1/Pl))/Count(*))*100, 1) as 'ExpectedWinrate',
Players.Pname as 'Player'
FROM
Wins, Players
WHERE Code LIKE CONCAT('%', Players.SC, '%')
GROUP BY Players.Pname) a
INNER JOIN
(SELECT
Count(*) as 'Wins',
Players.Pname as 'Players'
FROM Players, Wins
WHERE Players.PNr = Wins.PNr
GROUP BY Players.Pname
ORDER BY Count(*) desc) b ON 'Players' = 'Player';
我遇到的问题是我在一个查询中需要Count(*)两个不同的东西,所以我必须创建两个独立的并加入它们,但我不知道如何“命名”他们(在这种情况下我尝试使用“a”和“b”)以使用 a。'播放器', a。'ExpectedWinrate'等表达式。
任何人都可以帮助MYSQL newb吗?^^
greetzSP
编辑:添加了示例表...
CREATE TABLE Players
(
PNr int primary key,
Pname varchar(20),
SC varchar(1)
);
INSERT INTO Players
(PNr, Pname, SC)
VALUES
(1, 'Tobias', 'T'),
(2, 'Alex', 'A'),
(3, 'Martin', 'M'),
(4, 'Maria', 'R');
CREATE TABLE Wins
(
PNr int,
DNr int,
Pl int,
Code varchar(10)
);
INSERT INTO Wins
(PNr, DNr, Pl, Code)
VALUES
(1, 13, 3, 'ATM'),
(4, 1, 4, 'RTMA'),
(3, 20, 3, 'RTM');
胜出:(省略在此查询中无关紧要的列)
| PNR | DNR | PL | CODE |
|-----|-----|----|------|
| 1 | 13 | 3 | ATM |
| 4 | 1 | 4 | RTMA |
| 3 | 20 | 3 | RTM |
玩家:
| PNR | PNAME | SC |
|-----|--------|----|
| 1 | Tobias | T |
| 2 | Alex | A |
| 3 | Martin | M |
| 4 | Maria | R |
答案 0 :(得分:0)
SELECT a.Player ,a.ExpectedWinrate ,b.Wins
FROM(
SELECT
ROUND(((SUM(1/w.Pl))/Count(*))*100, 1) as 'ExpectedWinrate'
,p.Pname as 'Player'
FROM Wins w inner join Players p
on w.Code LIKE CONCAT('%', p.SC, '%')
GROUP BY p.Pname
) a
inner join
(
SELECT
Count(*) as 'Wins'
,p.Pname as 'Players'
FROM Players p inner join Wins w
on p.PNr = w.PNr
GROUP BY p.Pname
--ORDER BY Count(*) desc
) b
ON a.Player = b.Players
我在SQL Server上测试过它,试试MySQL