如何过滤预先查询

时间:2016-07-05 15:37:48

标签: sql sql-server tsql filter

SELECT 
    Staff.FullName, Skills.Name, Staff.Team, StaffSkills.Rating
FROM
    StaffSkills
INNER JOIN 
    Staff ON  Staff.Id = StaffSkills.StaffId
INNER JOIN 
    Skills ON Skills.Id = StaffSkills.SkillId
WHERE 
    Staff.Team LIKE '%financial services%' 
    AND (Skills.Name LIKE '%SDLC%' OR Skills.Name LIKE '%prince2%' OR  
         Skills.Name LIKE '%agile%' OR Skills.Name LIKE '%waterfall%' OR 
         Skills.Name LIKE '%oracle%' OR Skills.Name LIKE '%stakeholder%' OR 
         Skills.Name LIKE '%leadership%' OR Skills.Name LIKE '%negotiation%' OR  
         Skills.Name LIKE '%planning%')

我的目标是过滤结果,以便显示所有拥有“%project management%”技能的金融服务人员 - 我还希望结果显示更多技能(如果他们持有) - 所以任何事情在OR条款之后,如果他们持有一个在那里命名的技能,它应该显示在我的结果上。

很快插入预期结果,请耐心等待。

2 个答案:

答案 0 :(得分:1)

据我了解,您希望只看到作为金融服务项目经理的员工,但您也希望看到他们的其他技能。现在,您只是过滤以查看具有金融服务和项目管理的特定团队/技能的记录。如果您使用子查询为这些人拉ID,则可以返回他们的所有技能。像这样:

SELECT Staff.FullName, Skills.Name, Staff.Team, StaffSkills.Rating
FROM   StaffSkills
INNER JOIN Staff ON Staff.Id = StaffSkills.StaffId
INNER JOIN Skills ON Skills.Id  =StaffSkills.SkillId
WHERE Staff.ID in (
        SELECT Staff.ID
        FROM StaffSkills
        INNER JOIN Staff ON Staff.Id = StaffSkills.StaffId
        INNER JOIN Skills ON Skills.Id  =StaffSkills.SkillId
        WHERE Staff.Team LIKE '%financial services%' 
        AND Skills.Name LIKE '%project management%')

修改:您还应考虑尽可能将LIKE更改为=。或者,如果没有别的,只使用一个通配符%=LIKE 'abc%'可以使用索引。双通配符'%abc%'不会(完整的字符串搜索)。

答案 1 :(得分:0)

玩这个并看看它是否有帮助...我想出了只需在记事本上进行编辑....需要更多细节以及您的架构和规则才能在真实桌面上进行尝试。

SELECT Staff.FullName, Skills.Name, Staff.Team, StaffSkills.Rating, a.Name as AdditionalSkills
FROM StaffSkills
INNER JOIN Staff 
    ON Staff.Id = StaffSkills.StaffId
INNER JOIN Skills
    ON Skills.Id  =StaffSkills.SkillId
LEFT OUTER JOIN Skills a
    ON a.Id  = StaffSkills.SkillId
WHERE Staff.Team LIKE '%financial services%' AND Skills.Name LIKE '%project management%' OR
    (a.Name LIKE '%SDLC%' OR a.Name LIKE '%prince2%' OR
     a.Name LIKE '%agile%' OR a.Name LIKE '%waterfall%' OR
     a.Name LIKE '%oracle%' OR a.Name LIKE '%stakeholder%' OR
     a.Name LIKE '%leadership%' OR a.Name LIKE '%negotiation%' OR
     a.Name LIKE '%planning%')