MySQL计算左连接问题

时间:2015-02-10 21:10:15

标签: mysql sql

所以这是我的查询

SELECT 
    `ez_cell_ar_report`.`custno`,
    `ez_cell_ar_report`.`company`,
    count(net10sold.custno) as Net10_Sims_Sold,
    count(net10used.custno) as Net10_Sims_Used,
    (count(net10sold.custno) - count(net10used.custno)) as Net10_Sims_OnHand
from
    `ez_cell_ar_report`
LEFT join
    (select `ez_cell_detail_sales`.`custno` from `ez_cell_detail_sales` 
        where `ez_cell_detail_sales`.`item` = 'NET10SIM'
        or `ez_cell_detail_sales`.`item` = 'NET10SIMIS'
        or `ez_cell_detail_sales`.`item` = 'NET10NANOSIM'
        or `ez_cell_detail_sales`.`item` = 'NET10NANOSIMIS'
        or `ez_cell_detail_sales`.`item` = 'NET10SIMDUAL'
        OR `ez_cell_detail_sales`.`item` = 'NET10SIMFREE'
        )
        AS net10sold
    on net10sold.custno = `ez_cell_ar_report`.`custno`
LEFT JOIN
    ((select `activations_view`.`custno` from `activations_view` 
        inner join
            `net10_activations_view`
        on `activations_view`.`serial` = `net10_activations_view`.`sim`)
    union all (select `net10_simswap_view`.`custno` from `net10_simswap_view`
                where `net10_simswap_view`.`custno` is not null
        )) net10used
on net10used.custno = `ez_cell_ar_report`.`custno`
where

    `ez_cell_ar_report`.`state` IN ('MI','AL','AR','AZ','CA','CO','CT','DC','DE','FL','GA','HI','IA','ID','IL','IN')
    group by `ez_cell_ar_report`.`custno`,`ez_cell_ar_report`.`company`

我的问题是Net10_Sims_Sold始终与Net10_Sims_Used相同它应该是不同的它应该是与union all查询中的custno匹配的所有记录的计数。有人可以帮忙吗?

0 个答案:

没有答案