条件Where子句替代

时间:2013-02-18 09:25:06

标签: mysql xml

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

<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 个答案:

答案 0 :(得分:3)

尝试使用 AND

将其他条件(如Where子句pr.fActive = 1 ...)与'相对左连接子句放在一起

示例...

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

希望,这会有所帮助。

答案 1 :(得分: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 ...

答案 2 :(得分:2)

在Where子句中查看:

 cu.fStatus=1 OR ( pr.fActive=1 AND pr.fDeleted=0 AND wp.fStatus=1 AND act.fStatus=1)

答案 3 :(得分: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;