Sql 从3个不同的表进行查询
在下面的查询中,如何执行查询 其中,如果tbl1.ID中存在tbl3.CID或tbl2.ID中存在tbl3.CID,则警报为“” 然后“确认”或“输出”最终结果Sql 从3个不同的表进行查询,sql,sql-server,sql-server-2012,Sql,Sql Server,Sql Server 2012,在下面的查询中,如何执行查询 其中,如果tbl1.ID中存在tbl3.CID或tbl2.ID中存在tbl3.CID,则警报为“” 然后“确认”或“输出”最终结果 select * from tbl3, tbl4 where tbl3.type = tbl4.name and tgl4.GroupName <> 'abc' tbl1 ID Prime_Number 1 1-11 1 1-22 2 2-11 tbl2 ID Alert 1 NULL 2 NULL 3 X
select * from tbl3, tbl4 where tbl3.type = tbl4.name and tgl4.GroupName <> 'abc'
tbl1
ID Prime_Number
1 1-11
1 1-22
2 2-11
tbl2
ID Alert
1 NULL
2 NULL
3 XOM
tbl3
CID Sales
1 100
3 200
4 300
tbl4
Name GroupName
CORP Corporates
INTL International
存在
浮现在脑海中:
select t3.*,
(case when exists (select 1 from tbl1 t1 where t1.id = t3.cid)
then 'CONFIRM'
when exists (select 1 from tbl2 t2 where t2.id = t3.cid and t2.alert is not null)
then 'CONFIRM'
else 'OUT'
end) as result
from table3 t3;
如果您从
tbl3
和tbl4
中进行选择,为什么您希望从tbl1
和tbl2
中获得结果?
select t3.*,
(case when exists (select 1 from tbl1 t1 where t1.id = t3.cid)
then 'CONFIRM'
when exists (select 1 from tbl2 t2 where t2.id = t3.cid and t2.alert is not null)
then 'CONFIRM'
else 'OUT'
end) as result
from table3 t3;