0

我使用的是 Oracle 10,但提出这个问题的最好方法是举个例子。

select * 
  from t1, t2
 where t1.id = t2.id
   and t1.otherID = (select max(otherID) 
                       from t2 
                      where id = THE ID FROM THE OUTER QUERY T1
                    )

我想你知道我想做什么。我需要t1在子查询中引用以将其加入到t2.

我需要知道如何创建这样的查询。

“来自外部查询 T1 的 ID”是我的困惑所在。

我尝试使用t1.id,但没有得到结果。

4

1 回答 1

0

尝试以下

select t1.*, t2.*
from t1
join t2 on t1.id = t2.id
join (select id, max(otherID) as max_otherID
      from t2 
      group by id
 ) a ON a.id = t1.id and a.max_otherID = t1.otherID

在连接上使用子查询通常比在 where 子句中使用它提供更好的性能。

于 2013-04-26T21:35:41.980 回答