码迷,mamicode.com
首页 > 其他好文 > 详细

select top 1关联查父级名称

时间:2020-07-10 15:11:25      阅读:65      评论:0      收藏:0      [点我收藏+]

标签:sde   role   code   info   left join   HERE   data   erro   XML   

SELECT users.UserID AS Id,
       users.UserName,
       users.TrueName,
       users.Phone,
       users.Email,
       STUFF(
       (
           SELECT , + roles.Description
           FROM Accounts_UserRoles userroles
               LEFT JOIN Accounts_Roles roles
                   ON roles.RoleID = userroles.RoleID
           WHERE userroles.UserID = users.UserID
           FOR XML PATH(‘‘)
       ),
       1,
       1,
       ‘‘
            ) AS RoleNames,
       (
           SELECT TOP 1
                  UserName
           FROM Accounts_Users sup
           WHERE sup.UserID = users.SuperiorId
                 AND users.SuperiorId IS NOT NULL
       ) AS SuperiorName,
       users.Activity,
       users.IsSuperior,
       users.DataRange,
       dept.DeptName
FROM Accounts_Users users
    LEFT JOIN DeptInfo dept
        ON dept.DeptId = users.DeptID
           AND dept.DelFlag = 0
WHERE users.IsDelete = 0
      AND users.ClientCode IS NULL;

 

select top 1关联查父级名称

标签:sde   role   code   info   left join   HERE   data   erro   XML   

原文地址:https://www.cnblogs.com/shy1766IT/p/13279067.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!