【问题标题】:Oracle 11g correlated subquery returning multiple columnsOracle 11g 相关子查询返回多列
【发布时间】:2011-12-12 20:31:37
【问题描述】:

第一次查询获取ID和注册时间:

SELECT
  t1.mid
  t1.regtime

子查询需要去另一个表,SELECT address, city from t2 WHERE t2.mid = t1.mid AND MAX(t2.seqs)

t2 可能包含多个具有不同序列号的 mid。所以我们希望 mids 匹配,并且 seqs 是最高的。

问题:在 1 个子查询中返回多个列,同时获得最高的 t2.mid。

期望的最终结果:

 mid | regtime | address | city

【问题讨论】:

    标签: oracle oracle11g


    【解决方案1】:
    with t as 
        ( select t2.mid, address, city 
            from t2 
           where t2.seqs = ( select max(tt.seqs) 
                               from t2 tt
                              where tt.mid = t2.mid ) 
         ) 
    select t1.mid, t1.regtime, t.address, t.city from t1, t where t1.mid = t.mid
    

    应该可以。

    HTH

    【讨论】:

    • 非常感谢!今天学到了一些新东西。
    猜你喜欢
    • 2019-09-26
    • 1970-01-01
    • 2020-12-21
    • 2020-08-02
    • 2023-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多