簡體   English   中英

查詢左聯接而沒有B表中的所有右行

[英]Query left join without all the right rows from B table

我有2個表,A和B。我需要選擇A中的所有列+ B中的1列。 不幸的是,在聯接條件下,B在A中有1行具有多行(全部相同)。

我試過了,但是例如在保持選擇的同時,我無法通過左連接將A中的一行與B中的一行隔離。

我該如何查詢? 在ORACLE SQL中查詢

提前致謝。

這是outer apply的好用途。 查詢的結構如下所示:

select a.*, b.col
from a outer apply
     (select top 1 b.col
      from b
      where b.? = a.?
     ) b;

通常,您只會將top 1order by 在這種情況下,選擇哪一行似乎沒有什么不同。

您可以對A所有列進行group by ,然后使用匯總(例如maxmin )來選擇任何相同的B值:

select  a.*
,       b.min_col1
from    TableA a
left join
        (
        select  a_id
        ,       min(col1) as min_col1
        from    TableB
        group by
                a_id
        ) b
on      b.a_id = a.id

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM