[英]MYSQL Joins in Select query
我正在編寫一個數據庫來管理體育聯賽表,該數據庫需要一個單獨的表來保存偶爾出現的積分變化的細節,這些變化不在聯賽表計算的正常邏輯范圍內。
我有兩個表
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
我正在努力創建第二個聯接,以從積分調整表中引入數據以對聯賽表進行調整。
有人可以幫忙嗎?
最好的方法是為此替換子查詢“ 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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.