【发布时间】:2011-10-12 13:13:17
【问题描述】:
我这几天一直在为这个查询苦苦挣扎。我正在使用 PHP/MySQL。
这是一个系统,客户可以通过我正在为其开发的公司发送货运以进行收集或交付。我正在尝试编写的查询将收集为客户进行的收集和交付,并向他们发送他们当天花费的摘要。
下面有两个结果集,我需要将它们“连接在一起”才能发送报告。
(不要担心细节,问题在于连接 :))
交付结果集
此结果是公司代表所列客户交付的交付总额列表(按天分组)。
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
【问题讨论】:
-
你需要一个
FULL OUTER JOIN,这在 MySQL 中是不可用的。 -
请参阅this question,了解如何在 MySQL 中伪造完全外连接。
标签: php mysql join outer-join