簡體   English   中英

連接后如何從兩個表中獲取所有不同的ID?

[英]How to get all distinct id from two tables after join?

我想在連接后從兩個表中獲得所有不同的值。 拖曳表是..表:學生

Roll    Name
1       Anis
2       Badol
3       Chaity

表:考試

Roll    Quiz    Mid Final
1        15     20  40
3        25     20  42
4        10     12  8

我想得到那個輸出

Roll    Name    Mark
3      Chaity   87
1       Anis    75
4      Unknown  30
2      Badol    0

其中以“ Unknoun”代替的null中的名稱,由“ 0”代替的mark為null

我嘗試了這段代碼

select s.roll, name, (quiz+mid+final) as Mark
from student s
full outer join exam e on
s.roll=e.roll

並獲得輸出。

roll   name    mark
1      anis     75
2      badol    NULL
3     chaity    87
NULL    NULL    30

(對不起,英語不好)

一種方法使用full outer joincoalesce()

select coalesce(t1.roll, t2.roll) as roll,
       coalesce(t1.name, 'Unknown') as name,
       (coalesce(quiz, 0) + coalesce(mid, 0) + coalesce(final, 0)) as mark
from t1 full outer join
     t2
     on t1.roll = t2.roll
order by mark desc;

暫無
暫無

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

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