我有一个如下查询,我想给这个结果表一个别名,那我怎么能这样做呢?
这里我使用了两个带有union函数的子查询
(
select op.order_id,op.product_id,op.quantity as ordered_quantity, sum(odp.quantity) as delivered_quantity
from oops_order_product op
left join oops_order_delivery_product odp on odp.product_id = op.product_id and odp.id is null
where op.status = 0
group by op.product_id
)
union all
(
SELECT op.order_id,odp.product_id,sum(op.quantity) as ordered_quantity, sum(odp.quantity) as delivered_quantity
FROM `oops_order_delivery_product` odp
join oops_order_delivery od on od.id = odp.order_delivery_id
left join oops_order_product op on op.product_id = odp.product_id and op.id is null
where odp.status = 0 group by odp.product_id
)
答案 0 :(得分:1)
如果我正确理解你的问题,你想要这样的事情:
SELECT * FROM
(
select op.order_id,op.product_id,op.quantity as ordered_quantity, sum(odp.quantity) as delivered_quantity
from oops_order_product op
left join oops_order_delivery_product odp on odp.product_id = op.product_id and odp.id is null
where op.status = 0
group by op.product_id
union all
SELECT op.order_id,odp.product_id,sum(op.quantity) as ordered_quantity, sum(odp.quantity) as delivered_quantity
FROM `oops_order_delivery_product` odp
join oops_order_delivery od on od.id = odp.order_delivery_id
left join oops_order_product op on op.product_id = odp.product_id and op.id is null
where odp.status = 0 group by odp.product_id
)
as FinalTable