我需要您的帮助,因为我不知道该怎么做:
我有三个MySQL表:
pcs_clubs ID,简称
pcs_candidates id,id_compet,id_club,id_season
pcs_matchs id,id_candidate_dom,id_candidate_ext,id_day,date_day,buts_dom,buts_ext,id_season,id_compet,验证
样本数据:
pcs_clubs
id shortname
1 Barcelona
2 Real Madrid
3 Sevilla
4 Deportivo
5 Espanyol
6 Real Zaragoza
pcs_candidates
id id_compet id_club id_season
1 1 1 1
2 1 2 1
3 1 3 1
4 1 4 1
5 1 5 1
6 1 6 1
pcs_matchs
+----+------------------+------------------+--------+------------+----------+----------+-----------+-----------+------------+
| id | id_candidate_dom | id_candidate_ext | id_day | date_match | buts_dom | buts_ext | id_season | id_compet | validation |
+----+------------------+------------------+--------+------------+----------+----------+-----------+-----------+------------+
| 1 | 1 | 6 | 1 | 2015-11-12 | 0 | 3 | 1 | 1 | 1 |
+----+------------------+------------------+--------+------------+----------+----------+-----------+-----------+------------+
| 2 | 2 | 5 | 1 | 2015-11-12 | 2 | 2 | 1 | 1 | 1 |
+----+------------------+------------------+--------+------------+----------+----------+-----------+-----------+------------+
| 3 | 3 | 4 | 1 | 2015-11-12 | 2 | 1 | 1 | 1 | 1 |
+----+------------------+------------------+--------+------------+----------+----------+-----------+-----------+------------+
| 4 | 6 | 2 | 2 | 2015-11-17 | 3 | 1 | 1 | 1 | 1 |
+----+------------------+------------------+--------+------------+----------+----------+-----------+-----------+------------+
| 5 | 4 | 1 | 2 | 2015-11-17 | 1 | 1 | 1 | 1 | 1 |
+----+------------------+------------------+--------+------------+----------+----------+-----------+-----------+------------+
| 6 | 5 | 3 | 2 | 2015-11-17 | 1 | 2 | 1 | 1 | 1 |
+----+------------------+------------------+--------+------------+----------+----------+-----------+-----------+------------+
| 7 | 3 | 6 | 3 | 2015-11-24 | 2 | 0 | 1 | 1 | 1 |
+----+------------------+------------------+--------+------------+----------+----------+-----------+-----------+------------+
| 8 | 2 | 4 | 3 | 2015-11-24 | 3 | 1 | 1 | 1 | 1 |
+----+------------------+------------------+--------+------------+----------+----------+-----------+-----------+------------+
| 9 | 1 | 5 | 3 | 2015-11-24 | 4 | 1 | 1 | 1 | 1 |
+----+------------------+------------------+--------+------------+----------+----------+-----------+-----------+------------+
下一个MySQL查询成功生成所需比赛和赛季的联赛表:
SELECT
shortname 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(GD) AS GD,SUM(Pts) AS Pts
FROM(
SELECT
id_candidate_dom Team,
1 P,
IF(buts_dom > buts_ext,1,0) W,
IF(buts_dom = buts_ext,1,0) D,
IF(buts_dom < buts_ext,1,0) L,
buts_dom F,
buts_ext A,
buts_dom-buts_ext GD,
CASE WHEN buts_dom > buts_ext THEN 3 WHEN buts_dom = buts_ext THEN 1 ELSE 0 END PTS
FROM pcs_matchs
UNION ALL
SELECT
id_candidate_ext,
1,
IF(buts_dom < buts_ext,1,0),
IF(buts_dom = buts_ext,1,0),
IF(buts_dom > buts_ext,1,0),
buts_ext,
buts_dom,
buts_ext-buts_dom GD,
CASE WHEN buts_dom < buts_ext THEN 3 WHEN buts_dom = buts_ext THEN 1 ELSE 0 END
FROM pcs_matchs WHERE validation=1 AND id_compet=1 AND id_season=1
) as tot
JOIN pcs_candidates t ON tot.Team=t.id_club
JOIN pcs_clubs c ON c.id=t.id_club
GROUP BY Team
ORDER BY SUM(Pts) DESC, SUM(GD) DESC;
当前结果:
+------+---------------+---+---+---+---+---+----+-----+------------+
| | Team | P | W | D | L | F | A | GD | Pts |
+------+---------------+---+---+---+---+---+----+-----+------------+
| 1 | Sevilla | 3 | 3 | 0 | 0 | 6 | 2 | 4 | 9pts |
+------+---------------+---+---+---+---+---+----+-----+------------+
| 2 | Real Zaragoza | 3 | 2 | 0 | 1 | 6 | 3 | 3 | 6pts |
+------+---------------+---+---+---+---+---+----+-----+------------+
| 3 | Real Madrid | 3 | 1 | 1 | 1 | 6 | 6 | 0 | 4pts |
+------+---------------+---+---+---+---+---+----+-----+------------+
| 4 | Barcelona | 3 | 1 | 1 | 1 | 5 | 5 | 0 | 4pts |
+------+---------------+---+---+---+---+---+----+-----+------------+
| 5 | Deportivo | 3 | 0 | 1 | 2 | 3 | 6 | -3 | 1pts |
+------+---------------+---+---+---+---+---+----+-----+------------+
| 6 | Espanyol | 3 | 0 | 1 | 2 | 4 | 8 | -4 | 1pts |
+------+---------------+---+---+---+---+---+----+-----+------------+
我想根据当前日期之前每支球队的前5场比赛,在联赛表中增加一列(当前表格)。我只需要简单地在每场比赛中添加一个角色,如果球队获胜,那么W,如果平局,然后D,如果输球,那么L。(如果一支球队还没有参加5场比赛,那么就不向该列添加角色)这个:
所需结果:
+---+---------------+---+---+---+---+---+---+----+------+-------------------------------------+
| | | | | | | | | | | Form (from newest result to oldest based on date of match) |
+---+---------------+---+---+---+---+---+---+----+------+-------------------------------------+
| 1 | Sevilla | 3 | 3 | 0 | 0 | 6 | 2 | 4 | 9pts | WWW |
+---+---------------+---+---+---+---+---+---+----+------+-------------------------------------+
| 2 | Real Zaragoza | 3 | 2 | 0 | 1 | 6 | 3 | 3 | 6pts | LWW |
+---+---------------+---+---+---+---+---+---+----+------+-------------------------------------+
| 3 | Real Madrid | 3 | 1 | 1 | 1 | 6 | 6 | 0 | 4pts | WLD |
+---+---------------+---+---+---+---+---+---+----+------+-------------------------------------+
| 4 | Barcelona | 3 | 1 | 1 | 1 | 5 | 5 | 0 | 4pts | WDL |
+---+---------------+---+---+---+---+---+---+----+------+-------------------------------------+
| 5 | Deportivo | 3 | 0 | 1 | 2 | 3 | 6 | -3 | 1pts | LDL |
+---+---------------+---+---+---+---+---+---+----+------+-------------------------------------+
| 6 | Espanyol | 3 | 0 | 1 | 2 | 4 | 8 | -4 | 1pts | LLD |
+---+---------------+---+---+---+---+---+---+----+------+-------------------------------------+
https://www.flashscore.com/standings/pvZBCt9T/vHE9SNDE/#live
谢谢!
已更新查询(看起来正在运行)
SELECT
shortname 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(GD) AS GD,SUM(Pts) AS Pts, date_day AS dateday, group_concat(match_result ORDER BY date_day DESC)
FROM(
SELECT
id_candidate_dom Team,
1 P,
IF(buts_dom > buts_ext,1,0) W,
IF(buts_dom = buts_ext,1,0) D,
IF(buts_dom < buts_ext,1,0) L,
buts_dom F,
buts_ext A,
buts_dom-buts_ext GD,
CASE WHEN buts_dom > buts_ext THEN 3 WHEN buts_dom = buts_ext THEN 1 ELSE 0 END PTS,
CASE
WHEN buts_dom > buts_ext THEN 'W'
WHEN buts_dom = buts_ext THEN 'D'
WHEN buts_dom < buts_ext THEN 'L'
END match_result,
date_day
FROM pcs_matchs
UNION ALL
SELECT
id_candidate_ext,
1,
IF(buts_dom < buts_ext,1,0),
IF(buts_dom = buts_ext,1,0),
IF(buts_dom > buts_ext,1,0),
buts_ext,
buts_dom,
buts_ext-buts_dom GD,
CASE WHEN buts_dom < buts_ext THEN 3 WHEN buts_dom = buts_ext THEN 1 ELSE 0 END,
CASE
WHEN buts_dom < buts_ext THEN 'W'
WHEN buts_dom = buts_ext THEN 'D'
WHEN buts_dom > buts_ext THEN 'L'
END match_result,
date_day
FROM pcs_matchs WHERE validation=1 AND id_compet=1 AND id_season=1
) as tot
JOIN pcs_candidates t ON tot.Team=t.id_club
JOIN pcs_clubs c ON c.id=t.id_club
GROUP BY Team
ORDER BY SUM(Pts) DESC, SUM(GD) DESC
答案 0 :(得分:0)
将date_match
和match_result
列添加到UNION中的查询后,请在第一个SELECT子句中添加以下列:
group_concat(match_result ORDER BY date_match DESC)
对于比赛结果:
CASE
WHEN buts_dom > buts_ext THEN 'W'
WHEN buts_dom = buts_ext THEN 'D'
WHEN buts_dom < buts_ext THEN 'L'
END match_result