問:如何使sql查詢只返回1行?或者如何合併或2分的結果合併爲1
- 如何使合併或者2隊成績合併爲1?或
- 如何使它只返回1行?
表名:series_type
id| type| description
1 | 0| No series (Any team win 1 will be the winner)
2 | 1| Best of 3 (Any team wins 2 will be the winner else draw)
3 | 2| Best of 5 (Any team wins 3 will be the winner else draw)
表名:隊
ID| name
1848158| LV
15| LGD
表名:匹配
ID| series_id | series_type | league_id | start_time |radiant_name | dire_name | radiant_win
1 | 8313 | 2 | 2096 | xxxxxxx1 | LV | LGD | true
2 | 8313 | 2 | 2096 | xxxxxxx2 | LGD | LV | false
3 | 8313 | 2 | 2096 | xxxxxxx3 | LV | LGD | false
4 | 8313 | 2 | 2096 | xxxxxxx4 | LV | LGD | false
5 | 8313 | 2 | 2096 | xxxxxxx5 | LGD | LV | false
查詢:
SELECT series_id, team, SUM(Win) As Won, SUM(Loss) as Lost
FROM
(SELECT *,radiant_name as team,
CASE WHEN radiant_win = 1 THEN 1 ELSE 0 END as Win,
CASE WHEN radiant_win = 1 THEN 0 ELSE 1 END as Loss
FROM matches
UNION ALL
SELECT *,dire_name as team,
CASE WHEN radiant_win = 0 THEN 1 ELSE 0 END as Win,
CASE WHEN radiant_win = 0 THEN 0 ELSE 1 END as Loss
FROM matches
) as temp
WHERE series_id = 8313
GROUP By team
ORDER By Won, Lost DESC
結果:當由series_id組
series_id| team| Won| Lost|
8313| LV| 5| 5|
結果:當由茶組米
series_id| team| Won| Lost|
8313| LGD| 2| 3|
8313| LV| 3| 2|
預期結果:我需要它按series_id
series_id| teamA| teamB| teamAWon| teamBWon| teamALost| teamBLost|
8313| LV| LGD| 3| 2| 2| 3|
我回答,但你可以請把你的表結構,你想要的結果的一個明確的說法?另外,系列賽中還有兩支以上的球隊嗎?或者是兩支球隊在同一場比賽中進行比賽的每一場比賽? – jscott 2014-11-22 03:45:53
爲什麼要包含原始表格和結構? – Edrich 2014-11-22 03:51:16
請再次審查。謝謝 – ronscript 2014-11-22 04:01:47