我有一個運動隊成績的MYSQL表。每個事件或匹配都存儲有for
和against
目標得分值。我想要做的是檢索有序列表匹配順序目標(升序)。SQL加入並整理
這似乎很簡單,直到球隊的問題是客隊:
在這種情況下,我們正在尋找目標的
for
。當有問題的球隊是主隊時,我們正在看着目標'反對'。
我寫了下面的查詢:
(SELECT * FROM `matches`,`teams`,`outcomes`
WHERE `home_team_id`=11 AND `matches`.away_team_id=`teams`.team_id
AND `matches`.score_id=`outcomes`.outcome_id
ORDER BY `against`,`date` DESC LIMIT 0,20)
UNION
(SELECT * FROM `matches`,`teams`,`outcomes`
WHERE `away_team_id`=11 AND `matches`.home_team_id=`teams`.team_id
AND `matches`.score_id=`outcomes`.outcome_id
ORDER BY `for`,`date` DESC LIMIT 0,20)
它的工作原理,但結果集爲兩半,我想conceeded球隊是主場還是客場成績和順序組合。我需要一個別名來做到這一點嗎?
謝謝。
你能發表表格結構嗎?我明白你要做什麼,但沒有結構就有太多的猜測 - 例如我不確定'for','against'和'date'是否在'matches'或'outcome'中。 –