2012-09-12 78 views
0
Select c1.Eventid, c1.Place, c1.competitornum, c2.competitornum 
From Results c1, Results c2 
Where c1.Place = c2.Place 
and c1.Eventid = c2.Eventid 
and c1.competitornum < c2.competitornum 

以下查詢允許綁定事件的競爭對手從假設表'results'中顯示。即在事件中獲得相同的位置。有關向現有查詢中添加更多信息的sql查詢

我需要擴展查詢以顯示有關該事件的更多詳細信息 - 並取代事件ID,我需要從假設的其他/第二個表「事件」中顯示eventgender,距離和樣式。

如何擴展查詢以包含Events表中的必要信息?

+0

這裏很遠,但這是功課,不是嗎?我最近見過很多類似的查詢和表格結構... –

回答

1

要顯示來自相關表的數據,您可以JOIN他們。

Select e.eventgender, e.distance, e.style, 
     c1.Place, c1.competitornum, 
     c2.competitornum 
From Results c1, Results c2 
LEFT OUTER JOIN Events e ON e.Eventid = c1.Eventid 
Where c1.Place = c2.Place 
and c1.Eventid = c2.Eventid 
and c1.competitornum < c2.competitornum 
1

我假設在事件表中有一個Eventid列。

SELECT X.*, e.eventgender, e.distance , e.style 
FROM Events e 
INNER JOIN 
     (Select c1.Eventid, c1.Place, c1.competitornum, c2.competitornum 
     From Results c1, Results c2 
     Where c1.Place = c2.Place 
     and c1.Eventid = c2.Eventid 
     and c1.competitornum < c2.competitornum)X 
ON x.Eventid = e.Eventid