Where子句影响两个不同的select语句

时间:2016-04-19 20:32:01

标签: sql oracle oracle11g

我需要单独计算邮政编码为44444的付款人和凡人。他们的邮政编码存储在相同的表中,但他们的ID在多对多关系中的两个单独的表中。我写了这个:

select count(mortal_zip) as "Mortals in 44444"
      ,count(payer_zip) as "Payers in 44444"
  from
      (select am.zip_code as mortal_zip
         from address am
         join mortal_address
           on mortal_address.address_id = am.address_id
         join mortal
           on mortal.mortal_id = mortal_address.mortal_id
        where trim(am.zip_code) = '44444'
       ) m,
      (select ap.zip_code as payer_zip
         from address ap
         join payer_address
           on payer_address.address_id = ap.address_id
         join payer
           on payer.payer_id = payer_address.payer_id
        where trim(ap.zip_code) = '44444'
       ) p;

我知道:没有付款人的邮政编码为44444,但有3名凡人的邮政编码为44444.由于某种原因,我发现有0名凡人,邮政编码为44444.如果我问的话对于44444中的凡人,我得到了我需要的东西。如果我要求人数和444444的付款人数,我双方都会得到0。

另外,我尝试用子选择重写整个事情。

select count(m.mortal_zip) as "Mortals in 44444"
      ,count(p.payer_zip) as "Payers in 44444"
  from
      (select am.zip_code as mortal_zip
         from address am
        where trim(am.zip_code) = '44444'
          and am.address_id in
             (select mortal_address.address_id
                from mortal_address
               where mortal_address.mortal_id in
                  (select mortal.mortal_id
                     from mortal
                   )
              )
       ) m,
      (select ap.zip_code as payer_zip
         from address ap
        where trim(ap.zip_code) = '44444'
          and ap.address_id in
             (select payer_address.address_id
                from payer_address
               where payer_address.payer_id in
                    (select payer.payer_id
                       from payer
                    )
              ) 
       ) p;

我得到了相同的结果。

为什么where函数在另一个select语句中影响另一个select语句?

更新

我已经编写了查询但这两个查询返回的值不同。

此:

select m.mortal_zip as "Mortals in 44444"
      ,p.payer_zip as "Payers in 44444"
  from
      (select count(am.zip_code) as mortal_zip
         from address am
        where trim(am.zip_code) = '44444'
          and am.address_id in
             (select mortal_address.address_id
                from mortal_address
               where mortal_address.mortal_id in
                  (select mortal.mortal_id
                     from mortal
                   )
              )
       ) m,
      (select count(ap.zip_code) as payer_zip
         from address ap
        where trim(ap.zip_code) = '44444'
          and ap.address_id in
             (select payer_address.address_id
                from payer_address
               where payer_address.payer_id in
                    (select payer.payer_id
                       from payer
                    )
              ) 
       ) p;

返回:

Mortals in 44444 Payers in 44444
---------------- ---------------
               3               0

此:

select mortal_zip as "Mortals in 44444"
      ,payer_zip as "Payers in 44444"
  from
      (select count(am.zip_code) as mortal_zip
         from address am
         join mortal_address
           on mortal_address.address_id = am.address_id
         join mortal
           on mortal.mortal_id = mortal_address.mortal_id
        where trim(am.zip_code) = '44444'
       ) m,
      (select count(ap.zip_code) as payer_zip
         from address ap
         join payer_address
           on payer_address.address_id = ap.address_id
         join payer
           on payer.payer_id = payer_address.payer_id
        where trim(ap.zip_code) = '44444'
       ) p;

返回:

Mortals in 44444 Payers in 44444
---------------- ---------------
               5               0

3 个答案:

答案 0 :(得分:1)

您的查询正在尝试JOIN一个空结果集,其结果集包含三行。当然,这会返回空结果集。相反,您可以运行自己获取COUNT的子选择,或者:

SELECT
    COUNT(MA.address_id) AS "Mortals in 44444",
    COUNT(PA.address_id) AS "Payers in 44444"
FROM
    Address A
LEFT OUTER JOIN Mortal_Address MA ON MA.address_id = A.address_id
LEFT OUTER JOIN Payer_Address PA ON PA.address_id = A.address_id
WHERE
    A.zip_code = '44444'

答案 1 :(得分:0)

我建议使用UNION来获取所需的值,然后只需将值变为SUM:

SELECT SUM(MORTAL_ZIP) AS MORTAL_ZIP,
       SUM(PAYER_ZIP) AS PAYER_ZIP
FROM (select COUNT(*) as mortal_zip,
             0 as payer_zip
        from address a
        inner join mortal_address ma
           on ma.address_id = a.address_id
        inner join mortal m
           on m.mortal_id = ma.mortal_id
        where trim(a.zip_code) = '44444'
      UNION ALL
      select 0 as mortal_zip,
             count(*) as payer_zip
        FROM address a
        inner join payer_address pa
          on pa.address_id = a.address_id
        inner join payer p
          on p.payer_id = pa.payer_id
        where trim(a.zip_code) = '44444')

祝你好运。

答案 2 :(得分:-1)

我可能不需要问Stack Overflow,但这是我寻求的答案。希望它可以帮助谁可能在这个线程上发茬。

select m.mortal_zip as "Mortals in 44444"
      ,p.payer_zip as "Payers in 44444"
  from
      (select count(am.zip_code) as mortal_zip
         from address am
        where trim(am.zip_code) = '44444'
          and am.address_id in
             (select mortal_address.address_id
                from mortal_address
               where mortal_address.mortal_id in
                  (select mortal.mortal_id
                     from mortal
                   )
              )
       ) m,
      (select count(ap.zip_code) as payer_zip
         from address ap
        where trim(ap.zip_code) = '44444'
          and ap.address_id in
             (select payer_address.address_id
                from payer_address
               where payer_address.payer_id in
                    (select payer.payer_id
                       from payer
                    )
              ) 
       ) p;

我只需要将count放在子选择中。就像@TomH说的那样。