简体   繁体   中英

SQL Query for a possible self join?

I have a row with two id fields I.D_a, I.D_b, and a status field. Only the status for I.D_a exists in the row. I.D_b is I.D_a in another row where I.D_b is null. So I.D_b is I.D_a in another row whos I.D_b is null.

How can I pull the status of I.D_b when its status is in a different row where the value of I.D_b = I.D_a ?

Is this what you want?

select ta.*, tb.status as b_status
from t ta left join
     t tb
     on ta.id_b = tb.id_a and tb.id_b is null;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM