1

I'm wondering why is this code not returning any result

   SELECT A.PermissionGroupID, A.ApplicationID, CONVERT(nvarchar(5),A.PermissionID) AS Permission, A.PermissionName, B.PermissionGroupName, C.ApplicationName 
     FROM Permission AS A 
     JOIN PermissionGroup AS B ON A.PermissionGroupID = B.PermissionGroupID 
LEFT JOIN Application AS C ON A.ApplicationID = C.ApplicationID 
    WHERE (A.Active = 1) 
      AND (CONVERT(nvarchar(2),A.PermissionID) IN (SELECT Permissions 
                                                   FROM UserPermissions 
                                                   WHERE UserID = 1))

But this one is working

   SELECT A.PermissionGroupID, A.ApplicationID, CONVERT(nvarchar(5),A.PermissionID) AS Permission, A.PermissionName, B.PermissionGroupName, C.ApplicationName 
     FROM Permission AS A 
     JOIN PermissionGroup AS B ON A.PermissionGroupID = B.PermissionGroupID 
LEFT JOIN Application AS C ON A.ApplicationID = C.ApplicationID 
    WHERE (A.Active = 1) 
      AND (CONVERT(nvarchar(2), A.PermissionID) IN ('5','6','7','8'))

Here's the content of my Permission table

    Permission
--------------------------------
Permission   Permission    Application    Permission
ID           Group         ID             Name
4         1              1            VISA_APPLICATION_DELETE
5         1              1            VISA_APPLICATION_PRINT
6         4              10           APPLICATION_ADD   
7         4              10           APPLICATION_EDIT
8         4              10           APPLICATION_DELETE
9         4              10           APPLICATION_VIEW

Here's the content of my UserPermissions Table

UserPermissions
--------------------------
UserPermission      UserID    Permissions
ID
2           1         5,6,7,8   -> I tried to change it manually to this format ('5','6','7','8') but to no avail.
4

2 回答 2

1

为什么我怀疑权限看起来像一个字符串“5、6、7、8”。我希望查询是“PermisionId in (Select PermissionId from ...)”,而不是 (select Permissions)。

假设是这种情况,以下版本的查询应该可以解决您的问题:

SELECT A.PermissionGroupID, A.ApplicationID, CONVERT(nvarchar(5),A.PermissionID) AS Permission,
       A.PermissionName, B.PermissionGroupName, C.ApplicationName
FROM Permission A JOIN
     PermissionGroup B
     ON A.PermissionGroupID = B.PermissionGroupID LEFT JOIN
     Application AS C
     ON A.ApplicationID = C.ApplicationID cross JOIN
     (SELECT Permissions
      FROM UserPermissions
      WHERE UserID = 1
     ) p1
WHERE (A.Active = 1) AND
      charindex(','+CONVERT(nvarchar(2)+',', A.PermissionID)+',', ','+p1.Permissions+',') > 0

注意我在前面加上逗号,所以“1”不匹配“15”。此外,这假设每个用户在 UserPermissions 中只有一行。

顺便说一句,您应该修复您的架构,以便 UserPermissions 为每个用户和权限都有一个单独的行,这样您的原始公式就可以工作。

于 2012-05-21T14:54:14.967 回答
0

从评论中,我看到您担心性能。性能问题是由于您存储了一个逗号分隔的值字符串,其中每个值应该有一行。
把表UserPermissions改成这样。

create table UserPermissions
(
  UserID int references Users(UserID),
  PermissionID int references Permission(PremissionID),
  primary key (UserID, PermissionID)
)

你的查询应该是......

SELECT A.PermissionGroupID,
       A.ApplicationID, 
       A.PermissionID,
       A.PermissionName,
       B.PermissionGroupName,
       C.ApplicationName 
FROM Permission AS A 
  JOIN PermissionGroup AS B 
    ON A.PermissionGroupID = B.PermissionGroupID 
  LEFT JOIN Application AS C 
    ON A.ApplicationID = C.ApplicationID 
WHERE A.Active = 1 AND 
      A.PermissionID IN (SELECT PermissionID 
                         FROM UserPermissions 
                         WHERE UserID = 1)
于 2012-05-22T06:05:25.273 回答