php - MySQL 加入帮助

标签 php mysql join outer-join

几天来我一直在为这个查询而苦苦挣扎。我正在使用 PHP/MySQL。

这是一个系统,在这个系统中,客户可以发送 cargo ,由我为其开发的公司代收或送货。我尝试编写的查询将收集为客户执行的收集和交付,并向他们发送他们当天花费的摘要。

我在下面有两个结果集,我需要将它们“连接在一起”才能发送报告。

(不要担心具体细节,问题在于连接 :))

交付结果集

此结果是公司代表所列客户交付的总交付量列表(按天分组)。

summary_day, num_cons, num_spaces, customer_code 
2011-07-20      1       3           COB001P
2011-07-20      1       3           FAI001P
2011-07-20      2       2           FRE001P
2011-07-20      2       2           MIN001P
2011-07-20      17      29          NOR001P
2011-07-20      50      79          PAL001P
2011-07-20      1       1           PAR001P
2011-07-20      1       1           POT002P
2011-07-20      6       7           RHY001P
2011-07-20      9       13          TDG001P
2011-07-20      18      23          UPN001P

集合结果集

与上述类似,但相反,此结果是公司代表每个客户收集的总计列表。

2011-07-20  9   15  ARR001P
2011-07-20  1   1   BAC002P
2011-07-20  1   1   BLA001P
2011-07-20  4   6   CAR003P
2011-07-20  2   2   DIS001P
2011-07-20  2   2   DOV001P
2011-07-20  1   1   DRY001P
2011-07-20  1   1   ECC001P
2011-07-20  3   5   FAI001P
2011-07-20  2   2   INV001P
2011-07-20  2   2   MIN001P
2011-07-20  3   3   PAL001P
2011-07-20  1   1   QUA002P
2011-07-20  1   1   TEC002P
2011-07-20  1   1   THE006P
2011-07-20  7   7   WIL005P

问题

我正在努力使用 JOIN 来合并这两个结果集。

理想情况下,最终结果集应该是非常标准的,包含以下列:

summary_day, customer_code, num_deliveries, num_collections

数字字段将是每个结果集中的 num_spaces 列。如果客户同时有收货和发货记录,则显示这两个号码。如果他们有一个而不是另一个,我使用 COALESCE 来期望一列为 NULL,并相应地将其设置为 0。

我尝试在 customer_code 字段上使用 RIGHT OUTER JOIN,希望这会产生预期的结果,但我得到的唯一结果是:

2011-07-20, ARR001P, 0, 15
2011-07-20, BAC002P, 0, 1
2011-07-20, BLA001P, 0, 1
2011-07-20, CAR003P, 0, 6
2011-07-20, DIS001P, 0, 2
2011-07-20, DOV001P, 0, 2
2011-07-20, DRY001P, 0, 1
2011-07-20, FAI001P, 3, 5
2011-07-20, INV001P, 0, 2
2011-07-20, MIN001P, 2, 2
2011-07-20, PAL001P, 79, 3
2011-07-20, QUA002P, 0, 1
2011-07-20, TEC002P, 0, 1
2011-07-20, THE006P, 0, 1
2011-07-20, WIL005P, 0, 7

如您所见,结果集似乎只返回有收款的客户,这没问题,但我还需要查看有送货但没有收款的客户。

例如,一个例子是客户 NOR001P,他在交付结果集中但不在收款结果集中...

在这种情况下是否需要 FULL OUTER JOIN?如果是这样,考虑到 MySQL 不支持 FULL OUTER JOIN,我该如何解决这个问题?

感谢您花时间阅读。

完整解决方案

感谢CResults的回答,完整的解决方案如下... 可以看到,发送和收集结果集实际上是由子查询组成的,所以有点头疼!

set @summary_day = '2011-07-20';
select summary_day, customer_code, sum(num_deliveries) as pallet_deliveries, sum(num_collections) as pallet_collections
from
(
        select d.summary_day, d.customer_code, d.num_spaces as num_deliveries, 0 as num_collections from
            (select 
                        @summary_day as summary_day, /* change variable */
                        count(*) as num_cons,
                        sum( coalesce(micro_qty,0) + coalesce(quarter_qty,0) + coalesce(half_qty,0) + coalesce(full_qty,0) + coalesce(ceil(vlu_qty),0) ) as num_spaces,
                        pc.customer_code 
                    from pallet_routes pr
                    inner join pallet_consignments pc on pc.route_id = pr.route_id
                    where pr.date = @summary_day /* today */
                    and pc.type = 'D'
                    group by customer_code
            ) as d
        union all
        select c.summary_day, c.customer_code, 0 as num_deliveries, num_spaces as num_collections from
            (select 
                    @summary_day as summary_day, /* change variable */
                    count(*) as num_cons,
                    sum(coalesce(micro_qty,0) + coalesce(quarter_qty,0) + coalesce(half_qty,0) + coalesce(full_qty,0) + coalesce(ceil(vlu_qty),0)) as num_spaces,
                    pc.customer_code 
                from pallet_routes pr
                inner join pallet_consignments pc on pc.route_id = pr.route_id
                where pr.date = DATE_SUB(@summary_day, INTERVAL 1 DAY) /* Yesterday */
                and pc.type = 'C'
                group by customer_code
            ) as c
) as pallet_summaries
group by summary_day, customer_code

最佳答案

完全未经测试,但试一试

Select 
    Date, CustCode, Sum(Num_Cons) as Num_Cons, Sum(Num_Cols) as Num_Cols
from 
(    Select Date, CustCode, Num_Cons, 0 as Num_Cols From Consignments
    UNION ALL
    Select Date, CustCode, 0 as Num_Cons, Num_Cols From Collections
) parcels
group by Date, CustCode

关于php - MySQL 加入帮助,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6815841/

相关文章:

php - 显示 union select 语句的结果时出现问题

php - 类名始终为 StudlyCaps 还是 PSR-1 上有异常(exception)?

php - Laravel - 关于如何找出用户上次登录的架构

PHP - 有效的变量名

php - XPath和PHP:没有正常工作

mysql - cakephp 3 使用另一个表中的数据填充列表

php - 动态 laravel 数据库属性会影响应用程序速度吗?

php - SQL+CI : How to join multiple rows?

sql - 列表包含非聚合列

大表连接的mysql查询优化