下面的SQL小提琴示例:
http://sqlfiddle.com/#!9/75561/1
我在MySQL中有以下表格:
CREATE TABLE `straightred_fixture` (
`fixtureid` int(11) NOT NULL,
`fixturedate` datetime(6) DEFAULT NULL,
`fixturestatus` varchar(24) DEFAULT NULL,
`fixturematchday` int(11) NOT NULL,
`spectators` int(11) DEFAULT NULL,
`hometeamscore` int(11) DEFAULT NULL,
`awayteamscore` int(11) DEFAULT NULL,
`homegoaldetails` longtext,
`awaygoaldetails` longtext,
`hometeamyellowcarddetails` longtext,
`awayteamyellowcarddetails` longtext,
`hometeamredcarddetails` longtext,
`awayteamredcarddetails` longtext,
`awayteamid` int(11) NOT NULL,
`hometeamid` int(11) NOT NULL,
`soccerseasonid` int(11) NOT NULL,
PRIMARY KEY (`fixtureid`),
KEY `straightred_fixture_2e879a39` (`awayteamid`),
KEY `straightred_fixture_bcb6decb` (`hometeamid`),
KEY `straightred_fixture_d6d641f1` (`soccerseasonid`),
KEY `straightred_fixture_fixturematchday2_f98c3a75_uniq` (`fixturematchday`),
CONSTRAINT `D9b896edf0aff4d9b5c00682a8e21ea3` FOREIGN KEY (`fixturematchday`) REFERENCES `straightred_fixturematchday` (`fixturematchdayid`),
CONSTRAINT `straightr_soccerseasonid_92496b92_fk_straightred_season_seasonid` FOREIGN KEY (`soccerseasonid`) REFERENCES `straightred_season` (`seasonid`),
CONSTRAINT `straightred_fixtu_awayteamid_3d1961ba_fk_straightred_team_teamid` FOREIGN KEY (`awayteamid`) REFERENCES `straightred_team` (`teamid`),
CONSTRAINT `straightred_fixtu_hometeamid_6e37e94b_fk_straightred_team_teamid` FOREIGN KEY (`hometeamid`) REFERENCES `straightred_team` (`teamid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ;
如果可能的话,我想使用SQL查询实现的是团队表中的每个团队将前两个结果显示为W,L或D(赢得亏损或抽奖)以及他们与谁对战。我知道如果“fixturestatus”设置为“Finished”,游戏结束,并建立最近的两个,我可以按降序使用“fixturedate”字段。
输出可能类似于:
team id Game 1 Result Game 1 Opponent Game 2 Game 2 Opponent
12 W 15 D 45
13 L 45 L 36
以下是团队表的副本,如果它可以帮助解答:
CREATE TABLE `straightred_team` (
`teamid` int(11) NOT NULL,
`teamname` varchar(36) NOT NULL,
`country` varchar(36) DEFAULT NULL,
`stadium` varchar(36) DEFAULT NULL,
`homepageurl` longtext,
`wikilink` longtext,
`teamcode` varchar(5) DEFAULT NULL,
`teamshortname` varchar(24) DEFAULT NULL,
`currentteam` smallint(5) unsigned DEFAULT NULL,
PRIMARY KEY (`teamid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ;
任何帮助都会很棒,非常感谢,Alan。
答案 0 :(得分:1)
并非所有内容都可以通过sql完成,有时一些东西应该用编程语言完成,但是只要问题没有指定编程语言我提供了一些有用的视图和查询
CREATE VIEW united_result AS
SELECT fixtureid, fixturedate, fixturestatus, hometeamid as team,
awayteamid as opponent,
(CASE WHEN (hometeamscore-awayteamscore)>0 THEN 'W'
WHEN (hometeamscore-awayteamscore)<0 THEN 'L' ELSE 'D' END) as result,
'home' as mstatus
FROM straightred_fixture
UNION
SELECT fixtureid, fixturedate, fixturestatus, awayteamid as team,
hometeamid as opponent,
(CASE WHEN (hometeamscore-awayteamscore)<0 THEN 'W'
WHEN (hometeamscore-awayteamscore)>0 THEN 'L' ELSE 'D' END) as result,
'away' as mstatus
FROM straightred_fixture;
create or replace view plain_result as select fixtureid, fixturedate,
team, opponent, result, mstatus
from united_result where fixturestatus='Finished' order by team asc,
fixturedate desc;
select
team,
group_concat(
concat(result, ' against ', opponent, ' on ', date_format(fixturedate, '%D %M %Y'))
order by fixturedate desc separator ' | ') as output
from plain_result
where (select count(*)
from plain_result as p
where plain_result.team = p.team
and p.fixturedate>=plain_result.fixturedate) <= 2
group by team;