如何对此查询进行 select 大小写处理?

How can I do select case to this query?

SQL FIDDLE DEMO HERE

我有这样的 tables 结构:

    CREATE TABLE Users
    
            ([UserId] int, 
            [IdDepartment] int);
    
    INSERT INTO Users
        ([UserId], [IdDepartment])
    VALUES
        (1, 5),
        (2, 0),
        (3, -1),
        (4, 0),
        (5, -1),
        (6, 0);
    
    CREATE TABLE Department
        ([IdDepartment] int, [Name] varchar(23), [IdUser] int);
        
    INSERT INTO Department
        ([IdDepartment], [Name], [IdUser])
    VALUES
        (1, 'Sales', 3),
        (2, 'Finance', null ),
        (3, 'Accounting' , 5),
        (4, 'IT' ,3),
        (5, 'Secretary',null),
        (6, 'Sport',3);

我想获得包含以下结果的查询: 在 Users table 中,如果 IdDepartment 为 0,则表示该用户是管理员,因此他可以看到所有部门。如果用户在 idpartment 中有一个 -1,这意味着用户可以访问有限的部门,所以在这种情况下,我对部门 table 进行内部连接以获取该部门的列表。最后一种情况是,如果用户在用户 table 中的 idDepartament 编号与 0 和 -1 不同,则表示用户只能访问该部门。

我试过做类似的事情,但结构不合理:

select
    case idDepartment
       when  0 then (select Name from Department)
       when -1 then (select Name from Department where IdUser = 3)
       else         (select Name from Department 
                      inner join Users on Department.idDepartment = Users.Department         
                      where Users.UserId = 3)
    end
from 
    Department
where 
    IdUser = 3

我该怎么做?谢谢。

我想得到的我加个例子:

 -For the user that has the userid (1) -->

    Department Name
    ---------------
    Secretary




-For the user that has the userid (2) -->
    
    Department Name
    ---------------
    Sales
    Finance
    Accounting
    IT
    Secretary
    Sport




-For the user that has the userid (3) -->
    
    Department Name
    ---------------
    Sales
    IT
    Sports

你不能在 SELECT CASE 中做那样的事情,最好的选择是只引入一些逻辑

DECLARE @IdUser INT = 3
DECLARE @userDepartment INT
SELECT @userDepartment = IdDepartment
FROM Users
WHERE UserId = @IdUser

IF @userDepartment = 0
BEGIN
    SELECT Name FROM Department
END
ELSE IF @userDepartment = -1
BEGIN
   SELECT Name FROM Department WHERE IdUser = @IdUser
END
ELSE
BEGIN
    SELECT Name FROM Department 
    INNER JOIN Users 
       ON Department.idDepartment = Users.IdDepartment 
    WHERE Users.UserId = @IdUser
END

顺便说一句,您已经明白为什么您的结构不理想。如果您在用户和部门之间有一个连接点 table,您可以使用更简单的查询对您已有的任何组合进行建模(以连接点 table 中的大量行为代价)

您的示例代码有点混乱,但感觉您正在寻找这样的东西:

declare @id_user int = 3

select d.IdDepartment, d.Name
from Department d
where exists
  (
    select 1
    from Users u
    where u.[UserId] = @id_user
      and u.IdDepartment in (0, d.IdDepartment)
  )
  or d.[IdUser] = @id_user

它实现了:

  • 如果 Department 中的 IdUser table 与给定的 @id_user 相同 - 他肯定可以访问该部门
  • 否则,如果他的 IdDep 值为 0 或等于相应的部门 ID
  • ,则此用户可以访问部门

但是您的 permissions/security 模型闻起来不太好,而且绝对不可扩展。您最好发明另一个实体 (table) 来存储允许的元组:(IdUser, IdDepartment)。 Select 语句在这种情况下看起来会更清晰。

如果我从你的伪查询中了解到你想要做什么,你可以使用精心构造的 WHERE 子句来完成:

case idDepartment
   when 0 then (select Name from Department)
   when -1 then (select Name from Department where IdUser = 3)
   else (select Name from Department inner join Users on     Department.idDepartment = Users.Department where Users.UserId = 3)
end

可以写成子select:

(SELECT Name
 FROM Department
 LEFT OUTER JOIN Users
   on     Department.idDepartment = Users.Department
 WHERE idDepartment=0
 OR (idDepartment = -1 AND idUser = 3)
 OR (Users.UserId = 3)
)

当然,您必须将子select关联到您的外部查询。

  declare @IdUser int = 3;
  SELECT u.[UserId], d.Name 
  from Users u
  join Department d
        on   u.[IdDepartment] =  0 
        or ( u.[IdDepartment] = -1 and d.[IdUser]       = u.[UserId] )
        or ( u.[IdDepartment] >  0 and d.[IdDepartment] = u.[IdDepartment] )
   where u.[UserId] = @IdUser 
   order by u.[UserId], d.Name

userID 3 应该包括运动