我有一个RoleDetails
以此结构命名的表:
ROLE_ID - PK ,
NAME ,
PARENT_ROLE_ID FK ,
CREATED_BY_ID ,
MODIFIED_BY_ID
另一张桌子是user_details
USER_ID ,
USERNAME ,
FULLNAME
如何查询此表,以便在结果集中获得 RoleName、RoleId、ParentRoleId、ParentRoleName、CreatedByName 和 ModifiedByName。
到目前为止,我已经尝试过:
SELECT
[ROLE_ID] AS ID, r.NAME AS [RoleName],
r.PARENT_ROLE_ID AS [ParentRoleID] ,
(SELECT rd.NAME FROM dbo.ROLES rd
WHERE rd.ROLE_ID = r.PARENT_ROLE_ID ) AS [ParentRoleName],
CONCAT(ud.FIRST_NAME, ' ', ud.LAST_name) AS [CreatedByName] ,
CONCAT(u.FIRST_NAME, ' ', u.LAST_name) AS [LastModifiedByName]
FROM
dbo.ROLES r
LEFT OUTER JOIN
user_details u ON r.MODIFIED_BY = u.USER_ID
LEFT OUTER JOIN
dbo.USER_DETAILS ud ON r.CREATED_BY = ud.USER_ID
WHERE
r.ROLE_ID = @iRoleID;