SQL加入类似的SELECTS

时间:2017-04-12 07:53:00

标签: sql sql-server select join

我有两个类似的SELECT语句试图获得重叠,其中特定用户和另一个用户的数据集是相同的。我有以下但仍然收到错误:

  

Msg 8156,Level 16,State 1,Line 30     专栏' PermissionId'被多次指定为' T1'。

     

Msg 8156,Level 16,State 1,Line 40     专栏' PermissionId'为' T2'多次指定。

SELECT TOP 100 T1.Name, T1.permissionId
FROM (SELECT top 100 n.Name, rp.permissionId, rp.*
FROM dbo.[User] AS u
    JOIN dbo.UserRole AS ur ON ur.UserId = u.UserId
    JOIN dbo.ClientRole as cr on cr.RoleId = ur.RoleId
    JOIN dbo.Name as n2 on cr.NameId = n2.NameId 
    JOIN dbo.RolePermissions AS rp on rp.RoleId = ur.RoleId
    JOIN dbo.SystemTaxonomy AS st on st.SystemTaxonomyId = rp.PermissionId
    JOIN dbo.Name AS n on st.NameId = n.NameId

WHERE u.LoginName IN ('user1')) AS T1
JOIN (SELECT top 100 n1.Name, rp1.permissionId, rp1.*
FROM dbo.[User] AS u1
    JOIN dbo.UserRole AS ur1 ON ur1.UserId = u1.UserId
    JOIN dbo.ClientRole as cr1 on cr1.RoleId = ur1.RoleId
    JOIN dbo.Name as n2 on cr1.NameId = n2.NameId 
    JOIN dbo.RolePermissions AS rp1 on rp1.RoleId = ur1.RoleId
    JOIN dbo.SystemTaxonomy AS st1 on st1.SystemTaxonomyId = rp1.PermissionId
    JOIN dbo.Name AS n1 on st1.NameId = n1.NameId
WHERE u1.LoginName IN ('user2')) AS T2
ON T1.rp.permissionId = T2.rp1.permissionId

有人可以指导一下我的错误吗?

提前致谢

3 个答案:

答案 0 :(得分:2)

由于您使用的是派生表,因此必须使用唯一的列名。 您从第一个派生表中的public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; HttpSession session = request.getSession(false); if (session != null) { SessionInformation info = sessionRegistry.getSessionInformation(session .getId()); if (info != null) { if (info.isExpired()) { // Expired - abort processing doLogout(request, response); String targetUrl = determineExpiredUrl(request, info); if (targetUrl != null) { redirectStrategy.sendRedirect(request, response, targetUrl); return; } else { response.getWriter().print( "This session has been expired (possibly due to multiple concurrent " + "logins being attempted as the same user)."); response.flushBuffer(); } return; } else { // Non-expired - update last request date/time sessionRegistry.refreshLastRequest(info.getSessionId()); } } } chain.doFilter(request, response); } 和第二个衍生表中的PermissionId两次选择了rp列。
从派生表中删除显式rp1rp.permissionId,,您不应再次看到这些错误:

rp1.permissionId,

但是,我怀疑这是一个XYProblem。也许最好描述一下你的目标,而不是试图去实现目标。

答案 1 :(得分:1)

select语句不能显示多个具有相同名称的列。在select语句中使用rp.permissionId的别名

SELECT top 100 n.Name, rp.permissionId As 'Permission_Id', rp.*

另外,请检查最后一次加入条件

ON T1.rp.permissionId = T2.rp1.permissionId

工作正常。 rp& rp1是T1&的内部T2派生表。它们无法在外部识别。 您可以使用以下替换

ON T1.permissionId = T2.permissionId

答案 2 :(得分:0)

我用过:

SELECT TOP 100 T1.Name, T1.permissionId
FROM (SELECT top 100 n.Name, rp.*
FROM dbo.[User] AS u
    JOIN dbo.UserRole AS ur ON ur.UserId = u.UserId
    JOIN dbo.ClientRole as cr on cr.RoleId = ur.RoleId
    JOIN dbo.Name as n2 on cr.NameId = n2.NameId 
    JOIN dbo.RolePermissions AS rp on rp.RoleId = ur.RoleId
    JOIN dbo.SystemTaxonomy AS st on st.SystemTaxonomyId = rp.PermissionId
    JOIN dbo.Name AS n on st.NameId = n.NameId

WHERE u.LoginName IN ('user1')) AS T1
JOIN (SELECT top 100 n1.Name, rp1.*
FROM dbo.[User] AS u1
    JOIN dbo.UserRole AS ur1 ON ur1.UserId = u1.UserId
    JOIN dbo.ClientRole as cr1 on cr1.RoleId = ur1.RoleId
    JOIN dbo.Name as n2 on cr1.NameId = n2.NameId 
    JOIN dbo.RolePermissions AS rp1 on rp1.RoleId = ur1.RoleId
    JOIN dbo.SystemTaxonomy AS st1 on st1.SystemTaxonomyId = rp1.PermissionId
    JOIN dbo.Name AS n1 on st1.NameId = n1.NameId
WHERE u1.LoginName IN ('user2')) AS T2
ON T1.permissionId = T2.permissionId