如何使用sql连接两个表并创建一个新表来检查哪个表值存在

x6h2sr28  于 2021-07-24  发布在  Java
关注(0)|答案(1)|浏览(283)

我正在尝试连接两个表并创建一个新表,以便使用sql检查哪个表值存在
表a

Student ID   Name
1            abcd
2            efgh
3            ijkl
4            mnop
5            erst
6            uvwx

表b

Student ID   Name
1            qwer
2            west
3            ijkl
4            mnop
7            ikjh
8            iolk

输出为

Student ID   Name    New Column
1            abcd    Present in A
2            efgh    Present in A
3            ijkl    Both
4            mnop    Both
5            erst    Present in A
6            uvwx    Present in A
7            ikjh    Present in B
8            iolk    Present in B

请让我知道如何使用sql实现这一点。
提前谢谢!

xxhby3vn

xxhby3vn1#

这听起来像 FULL JOIN :

select id, name,
       (case when a.id is not null and b.id is not null then 'Both'
             when a.id is not null then 'A Only'
             else 'B Only'
        end) as new_column
from a full join
     b
     using (id, name)

相关问题