查询导致不包含值

时间:2014-05-16 16:29:34

标签: sql oracle

以下我在我公司的遗留数据库中查找值。

SELECT DISTINCT DLR.CIRCUIT_DESIGN_ID AS "CID",
                DLR.ECCKT AS "CIRCUIT",
                CI.LOCATION_ID_2 AS "SITE ID",
                CI.EXCHANGE_CARRIER_CIRCUIT_ID AS "ID",
                CI.RATE_CODE as "Rate",
                DLR.ACCESS_CUSTOMER_NAME AS "CUSTOMER SITE NAME",
                ADR.HOUSE_NBR || ' ' || ADR.STREET_NM || ' ' || ADR.STREET_SUF || ' ' || ADR.CITY_NAME || ' ' || ADR.STATE_CODE || ' ' || ADR.ZIP_CODE AS "CUSTOMER ADDRESS"
       FROM DESIGN_LAYOUT_REPORT DLR, CIRCUIT CI, MSAG_ADDR_LOC ADR
       WHERE CI.CIRCUIT_DESIGN_ID = DLR.CIRCUIT_DESIGN_ID 
             AND CI.LOCATION_ID_2 = ADR.LOCATION_ID
             AND CI.CIRCUIT_DESIGN_ID IN (
                 SELECT DISTINCT CIRCUIT_DESIGN_ID
                        FROM DLR_CIRCUIT_DESIGN_LINE
                        WHERE LOCATION LIKE '%  <some value from other code>   %'
                 )

我的问题来自于ADR只有我搜索的某些值的信息,导致此查询不包含我需要的所有值。 DLR和CI总是有价值。我如何使这个查询返回所有内容,只是给我一些空白的返回值与ADR条目不匹配?

1 个答案:

答案 0 :(得分:0)

您只需要一个外部联接:

SELECT DISTINCT DLR.CIRCUIT_DESIGN_ID AS "CID",
DLR.ECCKT AS "CIRCUIT",
CI.LOCATION_ID_2 AS "SITE ID",
CI.EXCHANGE_CARRIER_CIRCUIT_ID AS "ID",
CI.RATE_CODE as "Rate",
DLR.ACCESS_CUSTOMER_NAME AS "CUSTOMER SITE NAME",
ADR.HOUSE_NBR || ' ' || ADR.STREET_NM || ' ' || ADR.STREET_SUF || ' ' || ADR.CITY_NAME || ' ' || ADR.STATE_CODE || ' ' || ADR.ZIP_CODE AS "CUSTOMER ADDRESS"
FROM DESIGN_LAYOUT_REPORT DLR, CIRCUIT CI, MSAG_ADDR_LOC ADR
WHERE CI.CIRCUIT_DESIGN_ID = DLR.CIRCUIT_DESIGN_ID 
  AND CI.LOCATION_ID_2 = ADR.LOCATION_ID (+)
  AND CI.CIRCUIT_DESIGN_ID IN (
    SELECT DISTINCT CIRCUIT_DESIGN_ID
    FROM DLR_CIRCUIT_DESIGN_LINE
    WHERE LOCATION LIKE '%  <some value from other code>   %')

请注意额外的(+)。这是旧式连接,您现在应该使用ANSI连接。