按日期获取匹配结果

Posted

技术标签:

【中文标题】按日期获取匹配结果【英文标题】:Get match results by date 【发布时间】:2017-03-17 11:10:32 【问题描述】:

我得到了这个数据库:

如何按日期选择匹配项?我想要这样的输出:Barcelona (HomeTeam), 2 (HomeTeamScore), 2 (AwayTeamScore), PSG (AwayTeam)。

我想选择在某个日期进行的所有比赛,我需要根据进球球员从表格 PlayerMatch 中计算球队的结果(在这种情况下,外键 MatchId = 2(表格 PlayerMatch)梅西为巴塞罗那打进两球,而卡瓦尼为巴黎圣日耳曼打进 2 球,所以结果是 2:2)。

这里是创建表格和输入一些数据的代码:

CREATE TABLE Team(
TeamId          INT             IDENTITY PRIMARY KEY,
Name            NVARCHAR(75)    NOT NULL,
);

CREATE TABLE Players
(
PlayerId int identity Primary Key,
Name nvarchar(50) NOT NULL,
TeamId int,
FOREIGN KEY (TeamId) REFERENCES Team(TeamId)
);

CREATE TABLE Match
(
MatchId int identity Primary Key,
MatchDate date NOT NULL,
HomeTeamId int NOT Null,
AwayTeamId int NOT NULL,
FOREIGN KEY (HomeTeamId) REFERENCES Team(TeamId),
FOREIGN KEY (AwayTeamId) REFERENCES Team(TeamId)
);

CREATE TABLE PlayerMatch
(
PlayerMatchId int identity Primary key,
Goal bit not null,
PlayerId int not null,
MatchId int not null,
FOREIGN KEY (PlayerId) REFERENCES Players(PlayerId),
FOREIGN KEY (MatchId) REFERENCES Match(MatchId),
);

INSERT INTO Team (Name) VALUES ('Liverpool');
INSERT INTO Team(Name) VALUES ('Real Madrid');
INSERT INTO Team(Name) VALUES ('Barcelona');
INSERT INTO Team(Name) VALUES('PSG');

INSERT INTO Players(Name, TeamId) VALUES('Roberto Firmino', 1);
INSERT INTO Players(Name, TeamId) VALUES('Adam Lallana', 1);
INSERT INTO Players(Name, TeamId) VALUES('Luka Modric', 2);
INSERT INTO Players(Name, TeamId) VALUES('Benzema', 2);
INSERT INTO Players(Name, TeamId) VALUES('Messi', 3);
INSERT INTO Players(Name, TeamId) VALUES('Arda Turan', 3);
INSERT INTO Players(Name, TeamId) VALUES('Cavani', 4);
INSERT INTO Players(Name, TeamId) VALUES('Lucas', 4);

INSERT INTO Match(MatchDate, HomeTeamId, AwayTeamId) VALUES ('06-06-2007', 1, 2);
INSERT INTO Match(MatchDate, HomeTeamId, AwayTeamId) VALUES ('06-07-2007', 3, 4);


INSERT INTO PlayerMatch(Goal, MatchId, PlayerId) VALUES (1, 1, 1);
INSERT INTO PlayerMatch(Goal, MatchId, PlayerId) VALUES (1, 1, 2);
INSERT INTO PlayerMatch(Goal, MatchId, PlayerId) VALUES (1, 1, 3);

INSERT INTO PlayerMatch(Goal, MatchId, PlayerId) VALUES (1, 2, 5);
INSERT INTO PlayerMatch(Goal, MatchId, PlayerId) VALUES (1, 2, 5);
INSERT INTO PlayerMatch(Goal, MatchId, PlayerId) VALUES (1, 2, 7);
INSERT INTO PlayerMatch(Goal, MatchId, PlayerId) VALUES (1, 2, 7);

【问题讨论】:

你能显示你的预期输出吗 主队、主队得分、客队得分、客队得分。例如:巴塞罗那 - 2 - 2 - PSG 这个数据库设计有一个缺陷——当一个球员从 x 队转移到 y 队时你会怎么做?您必须保留包含开始日期和结束日期(可为空)的球队球员数据,以便您知道在比赛日期谁为哪支球队效力。如果您想要准确表示现实世界,情况会变得更加复杂,因为球员可能会被租借给球队。我已经通过在每场比赛中添加一个球员表格来解决这个问题,但是我的客户在比赛开始之前有人来测试这个表格的内容...... +++ 用于表格的格式和脚本/用于测试的插入 【参考方案1】:

你可以试试这个:

SELECT A.MATCHDATE, B1.NAME AS HOMETEAM, C1.GOAL AS GOAL_HOME, C2.GOAL AS GOAL_AWAY, B2.NAME AS AWAYTEAM
 FROM MATCH A
 INNER JOIN TEAM AS B1 ON A.HOMETEAMID=B1.TeamId
 INNER JOIN TEAM AS B2 ON A.AWAYTEAMID=B2.TeamId
 LEFT JOIN (SELECT MATCHID, TEAMID, COUNT(GOAL) AS GOAL
            FROM PLAYERMATCH 
            INNER JOIN PLAYERS ON PLAYERS.PlayerId=PLAYERMATCH.PlayerId
            GROUP BY MATCHID, TEAMID) C1 ON A.MATCHID = C1.MatchId AND A.HOMETEAMID=C1.TeamId
 LEFT JOIN (SELECT MATCHID, TEAMID, COUNT(GOAL) AS GOAL
            FROM PLAYERMATCH 
            INNER JOIN PLAYERS ON PLAYERS.PlayerId=PLAYERMATCH.PlayerId
            GROUP BY MATCHID, TEAMID) C2 ON A.MATCHID = C2.MatchId AND A.AWAYTEAMID=C2.TeamId

输出是:

MATCHDATE  HOMETEAM                                                                    GOAL_HOME   GOAL_AWAY   AWAYTEAM
---------- --------------------------------------------------------------------------- ----------- ----------- ---------------------------------------------------------------------------
2007-06-06 Liverpool                                                                   2           1           Real Madrid
2007-07-06 Barcelona                                                                   2           2           PSG

【讨论】:

谢谢!我需要从应用程序中传递 MatchDate,然后只选择在传递日期内播放的比赛。

以上是关于按日期获取匹配结果的主要内容,如果未能解决你的问题,请参考以下文章

按日期对 PHAsset 获取结果进行分组的智能方法

如何从按对象创建日期排序的核心数据中获取结果?

从按格式化日期排序的核心数据中获取

MySQL:按日期获取列行顺序的不同值

Impala:获取匹配分区列表

按最新日期获取分组后的列