需要将对手得分作为对手的别名而不是球队的队友

时间:2016-04-09 03:35:03

标签: mysql

我正在尝试运行一个显示支持和反对的团队分数的查询。告诉我它在表格中显示的内容,以便对手得分区域显示为“对手”的别名。

我无法使用mysqi解决问题,有人建议我需要创建一个函数来传递结果。我没有编写函数的经验,我已经尝试过研究如何,但没有运气。

我的查询

SELECT CONCAT(RoundNo, ',',Season) AS Game, Team, Total 
FROM MatchDetails  
WHERE Team = 'Team A' 
AND Season = 2015 
UNION
SELECT CONCAT(RoundNo, ',',Season) AS Game, Opponent, OTotal 
FROM MatchDetails  
WHERE Team = 'Team A'
AND Season = 2015

我当前的结果集

Game| Team  | Total
1 |Team A   | 125
2 |Team A   | 80
3 |Team A   | 78
4 |Team A   | 89
5 |Team A   | 102
1 |Team B   | 100
2 |Team C   | 79
3 |Team D   | 85
4 |Team E   | 87
5 |Team F   | 45

我想要的结果集

Game| Team  | Total
1 |Team A   | 125
2 |Team A   | 80
3 |Team A   | 78
4 |Team A   | 89
5 |Team A   | 102
1 |Opponent | 100
2 |Opponent | 79
3 |Opponent | 85
4 |Opponent | 87
5 |Opponent | 45

1 个答案:

答案 0 :(得分:0)

这是你想要的吗?

SELECT CONCAT(RoundNo, ',', Season) AS Game, Team, Total 
FROM MatchDetails  
WHERE Team = 'Team A' AND Season = 2015 
UNION ALL
SELECT CONCAT(RoundNo, ',', Season) AS Game, 'Opponent', OTotal 
FROM MatchDetails  
WHERE Team = 'Team A' AND Season = 2015;

区别在于Opponent周围的引号。