我正在尝试编写一个查询,如果 cust_no 位于两个表中,则输出“Y”,否则输出“N”。
Tab_1:
123
456
789
Tab_2:
123
456
896
输出:
123 Y
456 Y
789 N
我知道这个标准需要 case 语句,但不确定选择是如何发生的。如有任何提示,我们将不胜感激。
最佳答案
您可以使用完全联接
来执行此操作。
select coalesce(t1.num,t2.num) as num,
case when t1.num is not null and t2.num is not null then 'Y' else 'N' end
from tbl1 t1
full join tbl2 t2 on t1.num=t2.num
关于SQL查询比较2个表并输出case语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48211469/