简体   繁体   English

SQL Server查询:如何根据条件返回一组值?

[英]SQL Server query: how to return set of values based on condition?

Consider the following scenario (a bit contrived, but less complex than the real scenario I'm struggling with): 考虑以下场景(有些人为的,但是比我正在努力的真实场景要复杂的多):

A university with an online document management system wants to limit which professors can see which documents. 拥有在线文档管理系统的大学希望限制哪些教授可以查看哪些文档。 Some professors can see documents belonging to any department but some can only see documents belonging to specific departments. 有些教授可以查看任何部门的文档,但有些教授只能查看特定部门的文档。

Here's the schema: 这是模式:

create table Professors (
    ProfessorId int primary key,
    ProfessorName varchar(50)
);

create table Departments (
    DepartmentId int primary key,
    DepartmentName varchar(50)
);

create table ProfessorDepartments (
    ProfessorId int,
    DepartmentId int
);

insert into Professors values (1, 'Professor A'), (2, 'Professor B');
insert into Departments values (1, 'Chemistry'), (2, 'Computer Science'), (3, 'Math'), (4, 'Physics');
insert into ProfessorDepartments values (1,2), (1,3);

And here's the tricky part: a professor with unrestricted access will not have any departments listed in the ProfessorDepartments table . 这是棘手的部分:具有不受限制访问权限的教授将不会在ProfessorDepartments表中列出任何部门 (That way, said professor will automatically be given access to any new departments.) (这样,该教授将自动获得任何新部门的访问权限。)

How can I get the list of allowed departments for a specific professor? 如何获得特定教授的允许部门列表? The list needs to come from the ProfessorDepartments table if the professor has limited access and from the Departments table if the professor has unrestricted access. 如果教授访问受限,则该列表需要来自ProfessorDepartments表;如果教授访问不受限制,则需要来自Departments表。

SELECT
  Professors.ProfessorName,
  Departments.DepartmentName
FROM
  Professors
LEFT JOIN
  ProfessorDepartments
    ON Professors.ProfessorId = ProfessorDepartments.ProfessorId
LEFT JOIN
  Departments
    ON ProfessorDepartments.DepartmentId = Departments.DepartmentId
    OR ProfessorDepartments.DepartmentId  IS NULL
SELECT Professors.ID as ProfessorID, Departments.ID as DepartmentID, 
    Professors.ProfessorName, Departments.DepartmentName
FROM Professors
    LEFT OUTER JOIN ProfessorDepartments ON ProfessorDepartments.ProfessorID=Professors.ID
    LEFT OUTER JOIN Departments ON ProfessorDepartments.DepartmentID IS NULL OR 
        ProfessorDepartments.DepartmentID=Departments.ID

should do the trick 应该可以

Dems answer look like the best one. 答案似乎是最好的答案。

Here is my attempt. 这是我的尝试。

SELECT 
    p.ProfessorName, d.DepartmentName
FROM #Professors p
CROSS JOIN #departments d
WHERE NOT EXISTS (
    SELECT * FROM #ProfessorDepartments pd WHERE pd.ProfessorId = p.ProfessorId)

UNION ALL

SELECT p.ProfessorName, d.DepartmentName
FROM #Professors p
INNER JOIN #ProfessorDepartments pd
ON pd.ProfessorId = p.ProfessorId
INNER JOIN #Departments d
ON d.DepartmentId = pd.DepartmentId

I might not be fully understanding your question, but how about this: 我可能没有完全理解您的问题,但是如何解决:

SELECT P.ProfessorName
        ,CASE WHEN D.DepartmentName IS NULL THEN 'Unrestricted Access' ELSE D.DepartmentName END
FROM Professors P LEFT OUTER JOIN ProfessorDepartments PD ON
    P.ProfessorId = PD.ProfessorId
    LEFT OUTER JOIN Departments D ON
        PD.DepartmentId = D.DepartmentId

EDIT 编辑

After seeing Will's solution, I have updates my script: 看到Will的解决方案后,我更新了脚本:

SELECT P.ProfessorName, D.DepartmentName
FROM Professors P 
        LEFT OUTER JOIN ProfessorDepartments PD ON P.ProfessorId = PD.ProfessorId
        LEFT OUTER JOIN Departments D ON PD.DepartmentId IS NULL OR PD.DepartmentId = D.DepartmentId

You can do it like this: 您可以这样做:

declare @prof varchar(50)
select @prof = 'Professor B'

if(select count(pd.ProfessorId) from Professors p
    inner join ProfessorDepartments pd on p.ProfessorId = pd.ProfessorId
    where p.ProfessorName = @prof) = 0
begin
  select DepartmentName from Departments
end
else
begin
  select d.DepartmentName from Professors p
    inner join ProfessorDepartments pd on p.ProfessorId = pd.ProfessorId
    inner join Departments d on pd.DepartmentId = d.DepartmentId
    where p.ProfessorName = @prof
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM