简体   繁体   English

SQL Server-来自嵌入式的联接值不在查询中

[英]sql server - joining value from embedded not in query

I have the following query - 我有以下查询-

select a.applicationName 'Application Name', ug.shortName 'ITAM Owner', d.dbName 'Database Name', s.serverName 'Database Server Name' from tblApplications a
inner join tblUserGroups ug on ug.groupId = a.recordOwnerId
inner join tblApplicationDatabaseLinks adl on adl.applicationId = a.applicationId
inner join tblDatabases d on d.dbId = adl.dbId
inner join tblServers s on s.serverId = d.serverId
where a.applicationName = 'AppABC' and s.serverId not in (
select s.serverId from tblApplications a
inner join tblApplicationServerLinks asl on asl.applicationId = a.applicationId
inner join tblServers s on s.serverId = asl.serverId
where a.applicationName = 'AppABC')

It works fine on a one to one basis but I want to run it globally. 它在一对一的基础上运行良好,但我想在全球范围内运行它。

How do I effectively join the a.applicationName from the parent query to the a.applicationName from the subquery? 如何有效地joina.applicationName从父查询到a.applicationName从子查询?

I want it to look something like this: 我希望它看起来像这样:

select a.applicationName 'Application Name', ug.shortName 'ITAM Owner', d.dbName 'Database Name', s.serverName 'Database Server Name' from tblApplications a
inner join tblUserGroups ug on ug.groupId = a.recordOwnerId
inner join tblApplicationDatabaseLinks adl on adl.applicationId = a.applicationId
inner join tblDatabases d on d.dbId = adl.dbId
inner join tblServers s on s.serverId = d.serverId
where s.serverId not in (
select s.serverId from tblApplications a
inner join tblApplicationServerLinks asl on asl.applicationId = a.applicationId
inner join tblServers s on s.serverId = asl.serverId)

Which doesn't work because it's not knowing that the a.applicationId should match eachother. 这不起作用,因为它不知道a.applicationId应该彼此匹配。

Don't give the Applications table the same alias in both the subquery and the outer query. 不要在子查询和外部查询中为Applications表赋予相同的别名。 Instead of a , call one of them a1 or something like that. 而不是a ,调用其中一个a1或类似名称。

Then you can correlate the subquery to the outer query with the two different aliases. 然后,您可以将子查询与具有两个不同别名的外部查询相关联。

Answer was provided from the comments by @JoeC but for anyone else here is what I had to do: @JoeC的评论提供了答案,但对于其他人,我要做的是:

select ao.applicationName 'Application Name', ug.shortName 'ITAM Owner', d.dbName 'Database Name', s.serverName 'Database Server Name' from tblApplications ao
inner join tblUserGroups ug on ug.groupId = ao.recordOwnerId
inner join tblApplicationDatabaseLinks adl on adl.applicationId = ao.applicationId
inner join tblDatabases d on d.dbId = adl.dbId
inner join tblServers s on s.serverId = d.serverId
where s.serverId not in (
select s.serverId from tblApplications ai
inner join tblApplicationServerLinks asl on asl.applicationId = ai.applicationId
inner join tblServers s on s.serverId = asl.serverId
where ai.applicationId = ao.applicationId)
order by ao.applicationName asc

You can use a join or exists instead of the in clause. 您可以使用联接或存在来代替in子句。 weblogs.sqlteam.com/mladenp/archive/2007/05/18/60210.aspx weblogs.sqlteam.com/mladenp/archive/2007/05/18/60210.aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM