MS Access - SQL LEFT JOIN多个条件

时间:2016-05-18 13:16:45

标签: sql ms-access-2010 left-join

我有这个代码工作正常,除了我需要再添加一个条件:

SELECT     record1.*, 
           tbl_mpsregion.maintenanceteam, 
           tbl_mpsregion.regionmps 
INTO       tbl_sapforecast 
FROM       tbl_mpsregion 
RIGHT JOIN 
           ( 
                     SELECT    sap_ip19.*, 
                               dateserial(RIGHT(trim([SAP_IP19].[PlanDate]),4),mid(trim([SAP_IP19].[PlanDate]),4,2),LEFT(trim([SAP_IP19].[PlanDate]),2)) AS [DATE/FORECAST], 
                               tbl_labourstandard.re, 
                               tbl_labourstandard.manning, 
                               tbl_labourstandard.skillset AS skillset, 
                               tbl_regionmapping.maintenanceplant, 
                               tbl_regionmapping.area, 
                               tbl_regionmapping.region AS region, 
                               tbl_regionmapping.onresponse, 
                               [RE]*[Manning]/60 AS hours 
                     FROM      (sap_ip19 
                     LEFT JOIN tbl_labourstandard 
                     ON        ( 
                                         LEFT(sap_ip19.[Task list description],3) = tbl_labourstandard.jemenawc) 
                     AND       ( 
                                         sap_ip19.[MntPlan] = cdbl(tbl_labourstandard.supplypoint ))) 
                     LEFT JOIN tbl_regionmapping 
                     ON        sap_ip19.location = cdbl([Tbl_RegionMapping].[FittersDistricts])) AS record1 
ON         ( 
                      record1.region = [Tbl_MPSRegion].[Region]) 
AND        ( 
                      record1.skillset = [Tbl_MPSRegion].[Skillset]) ;

要添加的条件是:如果SAP_IP19.MntPlan与Tbl_LabourStandard.SupplyPoint不匹配,则使用0表示Tbl_LabourStandard.SupplyPoint。我没有使用Server 2000,所以使用CASE不是一个解决方案。尝试过IIF和SWITCH,但他们没有将查询带入睡眠模式(不进行评估)。我读到无法使用带有IIF或SWITCH的JOINS。请帮忙!

1 个答案:

答案 0 :(得分:0)

你应该可以添加if或者开关,但是你总是可以用OR来处理它 - 它不是最友好的但它应该完成工作,例如下面:

LEFT JOIN tbl_labourstandard
ON 
(LEFT(sap_ip19.[Task list description],3) = tbl_labourstandard.jemenawc) 
AND 
((Tbl_LabourStandard.SupplyPoint = SAP_IP19.MntPlan AND
sap_ip19.[MntPlan] = cdbl(tbl_labourstandard.supplypoint)) 
OR (sap_ip19.[MntPlan] = 0))