1

转换前查询

SELECT w.tech AS tech,
       w.wpcddate AS job_date,
       w.corp AS corp,
       w.house AS house_number,
       w.cust AS cust_number,
       h.zipcode AS zipcode
  FROM fsm_m_wipmaster w,
       fsm_m_zipmaster z,
       fsm_m_housemaster h,
       fsm_m_custmaster c
 WHERE c.corp(+) = w.corp
   AND c.house(+) = w.house
   AND c.cust(+) = w.cust
   AND h.corp(+) = w.corp
   AND h.house(+) = w.house
   AND h.cust(+) = w.cust
   AND z.corp(+) = w.corp
   AND z.zipbeg IS NULL
   AND h.zipcode IS NOT NULL
   AND w.tech IS NOT NULL
ORDER BY w.corp;

转换后查询

SELECT w.tech AS tech,
       w.wpcddate AS job_date,
       w.corp AS corp,
       w.house AS house_number,
       w.cust AS cust_number,
       h.zipcode AS zipcode
  FROM fsm_m_housemaster h
       LEFT OUTER JOIN
       fsm_m_custmaster c
      ON h.cust = c.cust
     AND h.corp_seq = c.corp_seq
     AND h.house = c.house
       LEFT OUTER JOIN
       fsm_m_wipmaster w
      ON h.cust = w.cust
     AND h.house = w.house
     AND h.corp_seq = w.corp_seq
       LEFT OUTER JOIN fsm_m_zipmaster z
      ON h.corp_seq = z.corp_seq AND z.zipbeg IS NULL
 WHERE w.tech IS NOT NULL
ORDER BY w.corp;

我似乎对为什么转换后的查询没有返回与之前的查询相同的结果。我知道我在这里遗漏了一些愚蠢的东西有人能发现明显的吗?

4

3 回答 3

4

原始查询仅包含一个外部联接,因为(+)尚未在相关表的所有条件上使用该外部联接。

因此,使用较新的连接语法的正确重写是:

SELECT w.tech AS tech,
    w.wpcddate AS job_date,
    w.corp AS corp,
    w.house AS house_number,
    w.cust AS cust_number,
    h.zipcode as zipcode
FROM fsm_m_wipmaster w 
JOIN fsm_m_zipmaster z   ON z.corp = w.corp AND z.zipbeg is null
JOIN fsm_m_housemaster h ON h.corp = w.corp AND h.house = w.house AND h.cust = w.cust AND h.zipcode is not null
LEFT OUTER JOIN fsm_m_custmaster c  ON c.corp = w.corp AND c.house = w.house AND c.cust = w.cust
WHERE w.tech IS NOT NULL
ORDER BY w.corp;
于 2012-12-20T14:53:13.183 回答
2

尝试 :

SELECT w.tech AS tech,
  w.wpcddate AS job_date,
  w.corp AS corp,
  w.house AS house_number,
  w.cust AS cust_number,
  h.zipcode as zipcode
FROM fsm_m_wipmaster w
LEFT OUTER JOIN fsm_m_zipmaster z ON w.corp = z.corp
LEFT OUTER JOIN fsm_m_housemaster h ON w.corp = h.corp AND w.house = h.house AND w.cust = h.cust
LEFT OUTER JOIN fsm_m_custmaster c ON w.corp = c.corp AND w.house = c.house AND w.cust = c.cust 
WHERE w.tech IS NOT NULL 
AND z.zipbeg IS NULL
AND h.zipcode IS NOT NULL
ORDER BY w.corp;

似乎您倒置JOINs了:在第一个查询中,它fsm_m_wipmaster是位于左侧的表LEFT OUTER JOIN(因此(+)在连接字段上)。

看到这个

于 2012-12-20T14:29:27.360 回答
1

一个原因可能是修改后的查询引用了一个名为z.corp_seqand的字段h.corp_seq,但原始查询没有:

left outer join fsm_m_zipmaster z on  h.corp_seq = z.corp_seq  and z.zipbeg is null 

试试这个:

left outer join fsm_m_zipmaster z on  h.corp = z.corp  and z.zipbeg is null 
于 2012-12-20T14:29:43.130 回答