Is it possible to get multiple values from a subquery?

后端 未结 7 889
無奈伤痛
無奈伤痛 2021-02-06 22:49

Is there any way to have a subquery return multiple columns in oracle db? (I know this specific sql will result in an error, but it sums up what I want pretty well)



        
7条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-06 23:05

    you can use cross apply:

    select
        a.x,
        bb.y,
        bb.z
    from
        a
        cross apply
        (   select b.y, b.z
            from b
            where b.v = a.v
        ) bb
    

    If there will be no row from b to mach row from a then cross apply wont return row. If you need such a rows then use outer apply

    If you need to find only one specific row for each of row from a, try:

        cross apply
        (   select top 1 b.y, b.z
            from b
            where b.v = a.v
            order by b.order
        ) bb
    

提交回复
热议问题