让我开始说我有以下两个表:
赛程表:
| straightred_fixture | CREATE TABLE `straightred_fixture` (
`fixtureid` int(11) NOT NULL,
`hometeamscore` int(11) DEFAULT NULL,
`awayteamscore` int(11) DEFAULT NULL,
`homegoaldetails` longtext,
`awaygoaldetails` longtext,
`awayteamid` int(11) NOT NULL,
`hometeamid` 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 |
团队表:
| straightred_team | 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 |
查询::
select hometeamid as team, awayteamid as opponent, hometeamscore as team_score, awayteamscore as opponent_score,
(case when (hometeamscore-awayteamscore)>0 then 'W' when (hometeamscore-awayteamscore)<0 then 'L' ELSE 'D' END) as result, 'home' as mstatus
from straightred_fixture sf, straightred_team st;
我想要做的是返回团队名称而不是上面查询中团队的ID。我意识到需要某种加入,但我让自己陷入了混乱。
答案 0 :(得分:1)
你需要加入球队表两次,一次是主队,一次是客队:
SELECT home.teamname AS team,
away.teamname AS opponent,
hometeamscore AS team_score,
awayteamscore AS opponent_score,
CASE WHEN (hometeamscore - awayteamscore) > 0 then 'W'
WHEN (hometeamscore - awayteamscore) < 0 then 'L'
ELSE 'D'
END AS result,
'home' AS mstatus
FROM straightred_fixture sf
JOIN straightred_team home ON hometeamid = home.teamid
JOIN straightred_team away ON awayteamid = away.teamid
答案 1 :(得分:0)
根据我的经验,我的建议可能是您不喜欢的(我将其作为答案,因为评论太长了。
在我看来/经验这里是你需要做的。你需要打破固定装置表。
灯具表应该具有特定于灯具本身的信息(日期,时间,可能是其他特定信息)
相关的表可以称为fixture_participants并包含团队信息 - 每个灯具在灯具参与者中有两行。
在我的解决方案中,我创建了一个主页/主页&#39;以H / A表示,以W / L表示的赢/输(现在考虑整数可能是更好的解决方案)。我认为这是欧洲足球所以你应该列出一个得分目标栏。
因此,您现在可以轻松识别主队和客队,您只需要撰写稍微复杂一些的查询,但您也可以说“A队在路上的平均进球数是多少?&#39;更容易。
PS - 如果您需要我正在谈论的this repository的样本为美国篮球建立类似的表关系