3

我正在使用 xml 来显示查询结果。我有一个与我的查询相关的问题。假设在客户表中我有一个数据,其 id 不是任何其他表中的外键,但我仍然想显示数据。ia,为此使用左连接。但问题在于其他表条件(Where 子句 pr.fActive=1...),因此我无法显示仅存在于客户表中但不存在于其他表中的数据。怎么做

<Table>customer cu</Table>
          <Joins>
            left join customerprogramxref cuprxref on cu.ixcustomer=cuprxref.ixcustomer
            left join tblprogram  pr on cuprxref.ixprogram=pr.ixprogram
            left join programworkpackagexref prwpxref on pr.ixprogram= prwpxref.ixprogram
            left join workpackage wp on wp.ixworkpackage =prwpxref.ixworkpackage
            left join workpackageactivityxref wpactxref on              wpactxref.ixworkpackage=wp.ixworkpackage
            left join activity act on act.ixactivity=wpactxref.ixactivity
          </Joins>
          <WhereClause>
            cu.fStatus=1 AND pr.fActive=1 AND pr.fDeleted=0 AND wp.fStatus=1 AND act.fStatus=1
          </WhereClause>
4

4 回答 4

3

尝试使用AND将其他条件(例如(Where 子句 pr.fActive=1...)与它的相对左连接子句放在一起)

例子...

LEFT JOIN tblprogram pr ON cuprxref.ixprogram = pr.ixprogram AND pr.fActive=1 AND pr.fDeleted=0

希望,这会有所帮助。

于 2013-02-18T09:31:20.290 回答
2

因为LEFT JOIN,在正确的部分你有 NULL 时没有加入。

试试这个:

cu.fStatus=1
        AND (pr.fActive=1 OR pr.fActive IS NULL)
        AND (pr.fDeleted=0 OR pr.fDeleted IS NULL)
        AND ...
于 2013-02-18T09:31:57.343 回答
2

在 Where 子句中检查这一点:

 cu.fStatus=1 OR ( pr.fActive=1 AND pr.fDeleted=0 AND wp.fStatus=1 AND act.fStatus=1)
于 2013-02-18T10:09:16.017 回答
2

您不能尝试使用嵌套查询:

像这样的东西,

SELECT finalMap.ixCustomer,finalMap.ixprogram, finalMap.ixWorkPackage,
FROM customer t
left outer join
(SELECT t.ixCustomer,cpRef.ixprogram, cpRef.ixWorkPackage
 FROM customerprogramxref t
left outer join
(SELECT pRef.ixprogram, pRef.ixWorkPackage FROM tblprogram pr
left outer join
(SELECT t.ixprogram,w.ixWorkPackage FROM programworkpackagexref t
left outer join
(SELECT wpa.ixWorkPackage FROM workpackage w
left outer join
(SELECT wpactxref.ixWorkPackage FROM workpackageactivityxref wpactxref
left outer join (SELECT t.ixactivity FROM activity t where t.fStatus=1) act
on act.ixactivity=wpactxref.ixactivity) wpa on w.ixworkpackage =wpa.ixworkpackage
where w.fStatus=1) w  on t.ixWorkPackage = w.ixWorkPackage) pRef
on pr.ixprogram = pRef.ixprogram
where pr.fActive=1  AND pr.fDeleted=0)cpRef
on t.ixprogram=cpRef.ixprogram)finalMap
on t.ixCustomer=finalMap.ixCustomer
where t.fStatus=1;
于 2013-02-18T11:46:45.920 回答