SQL - 检查记录存在于第二个表中,并将结果显示在主表中作为单独的列
我有2个表TAB1和TAB2如下:
TAB1 :
dog.h
TAB2 :
Sid Sname Svalue
===================
1 Joe 22
2 Derk 30
3 Derk1 40
结果应显示为:
id name SID
===================
1 Joe 2
2 Derk 3
任何人都可以构建查询吗?
答案 0 :(得分:1)
SELECT t1.Sid, t1.Sname, t1.Svalue, CASE WHEN t2.SID IS NULL THEN 'False' ELSE 'True' END AS Status
FROM Tab1 t1
LEFT OUTER JOIN Tab2 t2
ON t1.SID = t2.SID
答案 1 :(得分:0)
select
siid,sname,svaluee,
case when b.svalue is null then 'False' else 'true' end as status
from #t1 t1
outer apply
(
select t2.siid from #t2 t2 where t1.siid=t2.siid) b(svalue)
输出
siid sname svaluee status
1 Joe 22 False
2 Derk 30 true
3 Derk1 40 true