带有多个poules的

时间:2015-08-06 17:35:11

标签: php mysql

我正在为锦标赛创建一个网站。在网络的某个地方,我发现了一个sql语句,它包含2个表,游戏和团队,并输出锦标赛。

以下是团队表:

id  tname          poule
1   ZF Eger        heren1A
2   Pro Recco      heren1A
3   Sintez Kazan   heren1A
4   Szolnoki VSE   heren1A
5   Sintez Kazan 2 heren1B
6   Szolnoki VSE 2 heren1B

以下是游戏表:

id date hteam ateam hscore ascore gamefield poule played
1 2008-01-01 20:00:00 1 2 0 0 Veld 1 heren1A 0
2 2008-01-01 20:00:00 3 4 10 8 Veld 2 heren1A 1

下面的代码在数据库上执行sql-query并输出联盟排名。

$pouleA = 'heren1A';
$pouleB = 'heren1B';

$query = $mysqli->prepare('SELECT poule AS Poule, tname AS Team, Sum(WG) AS WG,Sum(W) AS W,Sum(G) AS G,Sum(V) AS V, SUM(DV) as DV,SUM(DT) AS DT,SUM(S) AS S,SUM(P) AS P FROM( SELECT hteam Team, 1 WG, IF(hscore > ascore,1,0) W, IF(hscore = ascore,1,0) G, IF(hscore < ascore,1,0) V, hscore DV, ascore DT, hscore-ascore S, CASE WHEN hscore > ascore THEN 3 WHEN hscore = ascore THEN 1 ELSE 0 END P FROM games WHERE played = 1 AND poule = ? OR poule = ? UNION ALL SELECT ateam, 1, IF(hscore < ascore,1,0), IF(hscore = ascore,1,0), IF(hscore > ascore,1,0), ascore, hscore, ascore-hscore S, CASE WHEN hscore < ascore THEN 3 WHEN hscore = ascore THEN 1 ELSE 0 END FROM games WHERE poule = ? OR poule = ?) as tot JOIN teams t ON tot.Team=t.id GROUP BY Team ORDER BY SUM(P) DESC, s DESC');

$query->bind_param('ssss', $pouleA, $pouleB, $pouleA, $pouleB);

此查询的问题在于,无论是否播放,它始终会对每个匹配进行计数。如何在我的查询中使用游戏列表中的播放列,以便只获得播放的匹配?

*编辑:

如果我将查询更改为:

SELECT poule AS Poule, tname AS Team, Sum(WG) AS WG,Sum(W) AS W,Sum(G) AS G,Sum(V) AS V, SUM(DV) as DV,SUM(DT) AS DT,SUM(S) AS S,SUM(P) AS P FROM( SELECT hteam Team, IF(played = 1,1,0) WG, IF(hscore > ascore,1,0) W, IF(hscore = ascore,1,0) G, IF(hscore < ascore,1,0) V, hscore DV, ascore DT, hscore-ascore S, CASE WHEN hscore > ascore THEN 3 WHEN hscore = ascore THEN 1 ELSE 0 END P FROM games WHERE played = 1 AND poule = ? OR played = 1 AND poule = ? UNION ALL SELECT ateam, 1, IF(hscore < ascore,1,0), IF(hscore = ascore,1,0), IF(hscore > ascore,1,0), ascore, hscore, ascore-hscore S, CASE WHEN hscore < ascore THEN 3 WHEN hscore = ascore THEN 1 ELSE 0 END FROM games WHERE played = 1 AND poule = ? OR played = 1 AND poule = ?) as tot JOIN teams t ON tot.Team=t.id GROUP BY Team ORDER BY SUM(P) DESC, s DESC

没有参加比赛的队伍没有被选中,但是我仍然希望他们被选中,因为我需要显示完整的poule,即使没有比赛也是如此。

1 个答案:

答案 0 :(得分:0)

试试这个:

$query = $mysqli->prepare('SELECT poule AS Poule, tname AS Team, Sum(WG) AS WG,Sum(W) AS W,Sum(G) AS G,Sum(V) AS V, SUM(DV) as DV,SUM(DT) AS DT,SUM(S) AS S,SUM(P) AS P FROM( SELECT hteam Team, 1 WG, IF(hscore > ascore,1,0) W, IF(hscore = ascore,1,0) G, IF(hscore < ascore,1,0) V, hscore DV, ascore DT, hscore-ascore S, CASE WHEN hscore > ascore THEN 3 WHEN hscore = ascore THEN 1 ELSE 0 END P FROM games WHERE played = 1 AND poule = ? OR poule = ? UNION ALL SELECT ateam, 1, IF(hscore < ascore,1,0), IF(hscore = ascore,1,0), IF(hscore > ascore,1,0), ascore, hscore, ascore-hscore S, CASE WHEN hscore < ascore THEN 3 WHEN hscore = ascore THEN 1 ELSE 0 END FROM games WHERE played = 1 AND poule = ? OR poule = ?) as tot JOIN teams t ON tot.Team=t.id GROUP BY Team ORDER BY SUM(P) DESC, s DESC');

在结束时使用FROM games WHERE poule = ? or poule = ?。我只是说了FROM games WHERE played = 1 AND poule = ? OR poule = ?