繁体   English   中英

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

[英]How to join two tables and create a new table to check in which table value exists using sql

我正在尝试连接两个表并创建一个新表以使用 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

Output 待定

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 来实现。

先感谢您 !

这听起来像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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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