我正在编写一个数据库来管理一个体育联赛表,这个表需要一个单独的tabe来保存不在联赛表计算正常逻辑之外的偶然点数变化的细节。
我有两张桌子
CREATE TABLE IF NOT EXISTS `fixtures` (
`id` int(6) unsigned NOT NULL AUTO_INCREMENT,
`comp_id` int(6) NOT NULL,
`date` date DEFAULT NULL,
`time` time DEFAULT NULL,
`hteam_id` int(6) DEFAULT NULL,
`ateam_id` int(6) DEFAULT NULL,
`hscore1` int(6) DEFAULT NULL,
`ascore1` int(6) DEFAULT NULL,
`hscore2` int(6) DEFAULT NULL,
`ascore2` int(6) DEFAULT NULL,
`hbonus` int(6) NOT NULL,
`abonus` int(6) NOT NULL,
`played` varchar(2) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=30 ;
CREATE TABLE IF NOT EXISTS `pointsadjust` (
`id` int(6) unsigned NOT NULL AUTO_INCREMENT,
`fix_id` int(6) DEFAULT NULL,
`team_id` int(6) DEFAULT NULL,
`value` int(6) DEFAULT NULL,
`reason` varchar(75) CHARACTER SET latin1 COLLATE latin1_general_ci DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=3 ;
我用来将联赛表拉到一起的查询是
SELECT
tname AS Team, Sum(P) AS P,Sum(W) AS W,Sum(D) AS D,Sum(L) AS L,
SUM(F) as F,SUM(A) AS A,SUM(SD) AS SD,SUM(BP) AS BP,SUM(Pts)+SUM(BP) AS Pts, SUM(((Pts+BP)*10000)+(W*100)+(SD*10)+((F/P)*1)) AS Rank
FROM(
SELECT
hteam_id Team,
1 P,
IF(hscore1 > ascore1,1,0) W,
IF(hscore1 = ascore1,1,0) D,
IF(hscore1 < ascore1,1,0) L,
hscore1 F,
ascore1 A,
hscore1-ascore1 SD,
hbonus BP,
CASE WHEN hscore1 > ascore1 THEN 2 WHEN hscore1 = ascore1 THEN 1 ELSE 0 END PTS
FROM fixtures WHERE comp_id = '1' AND played = 'N'
UNION ALL
SELECT
ateam_id,
1,
IF(hscore1 < ascore1,1,0),
IF(hscore1 = ascore1,1,0),
IF(hscore1 > ascore1,1,0),
ascore1,
hscore1,
ascore1-hscore1,
abonus,
CASE WHEN hscore1 < ascore1 THEN 2 WHEN hscore1 = ascore1 THEN 1 ELSE 0 END
FROM fixtures WHERE comp_id = '1' AND played = 'N'
) as tot
JOIN teams t ON tot.Team=t.id
GROUP BY Team
ORDER BY Rank DESC
我正在努力创建第二个连接来从点数调整表中引入数据以对联赛表进行调整。
有人可以帮忙吗?
答案 0 :(得分:0)
最好的方法是替换这个子查询“tot”的代码块,并指向新列“Pts_Adjst”(使用调整后的标点符号)而不是“Pts”(“香草” “标点符号”:
SQL小提琴(示例):http://sqlfiddle.com/#!9/c7a3c/14
SELECT
#tname AS Team,
Team TeamiD,
Sum(P) AS P,Sum(W) AS W,Sum(D) AS D,Sum(L) AS L,
SUM(F) as F,SUM(A) AS A,SUM(SD) AS SD,SUM(BP) AS BP,SUM(Pts_Adjst)+SUM(BP) AS Pts, SUM(((Pts_Adjst+BP)*10000)+(W*100)+(SD*10)+((F/P)*1)) AS Rank
FROM(SELECT tot.*, CASE WHEN pointsadjust.value is not null THEN pointsadjust.value ELSE PTS END PTS_ADJST, pointsadjust.reason FROM (
SELECT
hteam_id Team,
id Fix,
1 P,
IF(hscore1 > ascore1,1,0) W,
IF(hscore1 = ascore1,1,0) D,
IF(hscore1 < ascore1,1,0) L,
hscore1 F,
ascore1 A,
hscore1-ascore1 SD,
hbonus BP,
CASE WHEN hscore1 > ascore1 THEN 2 WHEN hscore1 = ascore1 THEN 1 ELSE 0 END PTS
FROM fixtures WHERE comp_id = '1' AND played = 'N'
UNION ALL
SELECT
ateam_id,
id Fix,
1,
IF(hscore1 < ascore1,1,0),
IF(hscore1 = ascore1,1,0),
IF(hscore1 > ascore1,1,0),
ascore1,
hscore1,
ascore1-hscore1,
abonus,
CASE WHEN hscore1 < ascore1 THEN 2 WHEN hscore1 = ascore1 THEN 1 ELSE 0 END
FROM fixtures WHERE comp_id = '1' AND played = 'N'
) as tot
LEFT JOIN pointsadjust
ON tot.Team = pointsadjust.team_id AND tot.Fix = pointsadjust.Fix_id) as tot
#JOIN teams t ON tot.Team=t.id
GROUP BY Team
ORDER BY Rank DESC