SQL Server基于连接的计算列

时间:2018-05-16 23:15:02

标签: sql sql-server

我有两张桌子:

表1:

id name
--------
1 Mark
2 Anna

表2:

id active_name
--------------
2 Anna

我想要第3张桌子或视图:

id name isActive
--------------------
1 Mark  No
2 Anna Yes

如何在SQL Server中执行此操作。

1 个答案:

答案 0 :(得分:4)

您可以使用left joincase表达式:

select t1.id, t1.name,
       (case when t2.id is null then 'No' else 'Yes' end) as isActive
from table1 t1 left join
     table2 t2
     on t2.id = t1.id and t2.name = t1.name;
相关问题