gpt4 book ai didi

mysql - 选择全部并集 - 为两个字段选择始终为零

转载 作者:行者123 更新时间:2023-11-30 00:45:16 25 4
gpt4 key购买 nike

我为我的 friend 编写了一个足球 table 程序,他们真的很想在其中包含更多统计数据。但我在红黑队的总胜负方面遇到了困难(W_S = 赢黑;L_S = 输黑)(W_R = 赢红;L_R = 输红)

这是我的架构和选择:

http://sqlfiddle.com/#!2/814a35/1/0

如您所见,W_S 和 L_S 始终为零,而 W_R 和 L_R 填充错误。

(schwarz = 黑色;rot = 红色;tore = 进球)

提前致谢!

编辑

全部SELECT query

SELECT 
`SpielerID` AS `id`,
`s`.`name` AS `name`,
COALESCE( (Sum(W_R) / NULLIF((Sum(W_R)+Sum(W_S))+(Sum(L_R)+Sum(L_S)), 0)), Sum(W_R) ) as 'rot_siegeRatio',
COALESCE( (Sum(W_S) / NULLIF((Sum(W_R)+Sum(W_S))+(Sum(L_R)+Sum(L_S)), 0)), Sum(W_S) ) as 'schwarz_siegeRatio',
Sum(W_R) as 'siegeCount', Sum(L_R) as 'niederlagenCount', (Sum(W_R)+Sum(L_R)) as 'spieleCount',
COALESCE( (Sum(W_R) / NULLIF((Sum(W_R))+(Sum(L_R)), 0)), Sum(W_R) ) as 'siegeRatio', Sum(Tore) as 'toreCount',
Sum(Gegentore) as 'gegentoreCount',
COALESCE( (Sum(Tore) / NULLIF(Sum(Gegentore), 0)), Sum(Tore) ) as 'toreRatio'
FROM
( SELECT rot_spieler1 SpielerID, datetime,
IF (rot_tore > schwarz_tore,1,0) W_R,
IF (rot_tore < schwarz_tore,1,0) L_R,
0 W_S,
0 L_S,
rot_tore Tore,
schwarz_tore Gegentore
FROM `kicker_spiele`
WHERE datetime IS NOT NULL

UNION ALL
SELECT rot_spieler2 SpielerID, datetime,
IF (rot_tore > schwarz_tore,1,0) W_R,
IF (rot_tore < schwarz_tore,1,0) L_R,
0 W_S,
0 L_S,
rot_tore Tore,
schwarz_tore Gegentore
FROM `kicker_spiele`
WHERE datetime IS NOT NULL

UNION ALL
SELECT schwarz_spieler1 SpielerID, datetime,
IF (schwarz_tore > rot_tore,1,0) W_S,
IF (schwarz_tore < rot_tore,1,0) L_S,
0 W_R,
0 L_R,
schwarz_tore Tore,
rot_tore Gegentore
FROM `kicker_spiele`
WHERE datetime IS NOT NULL

UNION ALL
SELECT schwarz_spieler2 SpielerID, datetime,
IF (schwarz_tore > rot_tore,1,0) W_S,
IF (schwarz_tore < rot_tore,1,0) L_S,
0 W_R,
0 L_R,
schwarz_tore Tore,
rot_tore Gegentore
FROM `kicker_spiele`
WHERE datetime IS NOT NULL
) as ERG
LEFT JOIN `kicker_spieler` `s` ON ERG.SpielerID = s.id
GROUP by ERG.SpielerID

最佳答案

我希望这对您有帮助:

SELECT *,
(SELECT CASE
WHEN rot_tore > schwarz_tore
THEN 1 ELSE 0
END
) AS W_R,
(SELECT CASE
WHEN schwarz_tore > rot_tore
THEN 1 ELSE 0
END
) AS L_R,
(SELECT CASE
WHEN schwarz_tore > rot_tore
THEN 1 ELSE 0
END
) AS W_S,
(SELECT CASE
WHEN rot_tore > schwarz_tore
THEN 1 ELSE 0
END
) AS L_S,
rot_tore Tore,
schwarz_tore Gegentore
FROM `kicker_spiele`
WHERE datetime IS NOT NULL

关于mysql - 选择全部并集 - 为两个字段选择始终为零,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21432478/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com