簡體   English   中英

使用 Join 和 Union 組合來自 2 個表的結果

[英]Using Join and Union to combine results from 2 tables

我已經有一個使用來自多個主表的連接的結果集,其中 TABLE A 作為主表。 現在我正在嘗試在表 B 上執行聯合,並保留來自主人的連接。

這是我嘗試過的查詢:

select 
    t1.* 
from 
    (select 
         id, mobile, email, pan 
     from a  
     union
     select  
         b_id, mobile, email, pan
     from b) as t1,
    ci.status,
    ab.desc
from 
    a 
left join 
    cuI ci on ci.id = a.id
left join 
    abMaster ab on ab.id = a.id
where 
    a.id is not null 
order by 
    a.created_on desc

這沒有用

乍一看你有幾個錯誤

某些列名ci.status, ab.descin錯誤的地方,

兩個從課

表 a 中的引用應該(可能)是對 t1 的引用

一個可能的有效查詢可能是

select  t1.* , ci.status, ab.desc
from 
    (select id, mobile, email, pan 
     from a  
     union
     select b_id, mobile, email, pan
     from b
) as t1 
left join 
    cuI ci on ci.id = t1.id
left join 
    abMaster ab on ab.id = t1.id
where 
    t1.id is not null 
order by 
    t1.created_on desc

暫無
暫無

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

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