下面的查询按照计划运行,它显示了我所连接的方式,这很好,但是问题是,如果您有更多的"specialization"表格,例如用户可能拥有多个数据的"Mail type"或其他任何内容... 您将不得不为每个表格进行两次左连接并通过
我想知道如何避免使用两个连接和在一个单独的连接中“赋予”
ISNULL
(在这种情况下)“赋予优先级”。我想知道如何避免使用两个连接和在一个单独的连接中“赋予”
TypeId 2
比TypeId 1
更高的优先级,是否可能?if object_id('tempdb..#Tab1') is not null drop table #Tab1
create table #Tab1 (UserId int, TypeId int)
if object_id('tempdb..#Tab2') is not null drop table #Tab2
create table #Tab2 (TypeId int, TypeDescription nvarchar(50))
insert into #Tab1 (UserId, TypeId)
values
(1, 1),
(1, 2)
insert into #Tab2 (TypeId, TypeDescription)
values
(1, 'User'),
(2, 'Admin')
select *, ISNULL(t2.TypeDescription, t3.TypeDescription) [Role]
from #Tab1 t1
LEFT JOIN #Tab2 t2 on t1.TypeId = t2.TypeId and
t2.TypeId = 2
LEFT JOIN #Tab2 t3 on t1.TypeId = t3.TypeId and
t3.TypeId = 1
top with ties
的存在...感谢您的解释。 - Veljko89