基于条件的内连接或左外连接

时间:2015-06-26 01:55:30

标签: sql-server join

我正在尝试在查询中添加一个条件来执行INNER JOIN或LEFT OUTER JOIN

这是两个查询

   USE [tisonline]

   SELECT TOP 1000 *
   FROM Jobs AS j
   LEFT OUTER JOIN JobQueries AS jq ON j.JobID = jq.JobID 
   LEFT OUTER JOIN Agents AS agt ON agt.AgentID = jq.AgentID
   where j.isMigrated = 1 

   SELECT TOP 1000 *
   FROM Jobs AS j
   INNER JOIN JobQueries AS jq ON j.JobID = jq.JobID 
   INNER JOIN Agents AS agt ON agt.AgentID = jq.AgentID
   where j.isMigrated = 0 

以下表格的架构:

Job: {
        [JobID]
        ,[JobGUID]
      ,[Duplicate]
      ,[CreateByTisForAgency]
      ,[TisClientCode]
      ,[AgencyID]
      ,[AgencyName]
      ,[BookingAgentID]
      ,[LanguageID]
      ,[ReqGender]
      ,[AnotherGender]
      ,[ProfessionalAccLevelReq]
      ,[InstructionsToInterpreter]
}
  JobQueryTable
  {
   [JobQueryID]
      ,[JobID]
      ,[JobGuid]
      ,[NonEnglishSpeakerName]
      ,[DuplicateJob]
      ,[JobDate]
      ,[JobStartTime]
      ,[JobEndTime]
      ,[JobState]
      ,[JobTier]
      ,[LanguageID]
      ,[AgencyID]
      ,[AgencyName]
      ,[AgentID]
   }
Agent Tabe 
{  
[AgentID]
      ,[AgentGUID]
      ,[Position]
      ,[Section]
      ,[Role]
      ,[AgentDetails_PersonalDetailsID]
      ,[Agency_AgencyID]
      ,[RecieveEmailUpdates]
      ,[ParticipateInTisSurvey]
      ,[RecieveSMSUpdates]
}

作业表和作业查询表具有基于作业ID的一对一关系。另外,作业查询表与基于代理ID的代理具有一对一的关系。但是对于作业表中的isMigrated为true,代理程序Id将始终为NULL。

我想根据ismigrated值将这两个查询组合成INNER JOIN或LEFT OUTER JOIN。迁移的作业在作业查询表中没有任何agentID。

如果需要进一步的详细信息,请与我们联系。

2 个答案:

答案 0 :(得分:3)

始终执行LEFT OUTER JOIN并添加WHERE条件以模拟INNER JOIN的{​​{1}}功能。

isMigrated = 0

答案 1 :(得分:2)

根据定义,filenames = glob.glob("*.xml") for filename in filenames: f = open(filename) ... 将执行OUTER JOIN的所有操作,然后执行更多操作。我会像这样修改你的INNER JOIN

LEFT OUTER JOIN