有关在现有查询中添加更多信息的SQL查询

时间:2012-09-12 07:20:39

标签: sql

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

以下查询允许在事件中绑定的竞争对手从假设表格“结果”中显示。即在事件中获得相同的位置。

我需要扩展查询以显示有关该事件的更多详细信息 - 并且代替事件ID,我需要显示假设的其他/第二个表,'事件'中的eventgender,distance和style。

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

2 个答案:

答案 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 :(得分:1)

我在Events表中假设有一个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