我正在从数据库中检索产品记录/销售订单。我正在使用MySQL的 UNION 功能加入表格结果。
如果我设置我的查询仅检索1个产品,则列的顺序看起来不错。附件是截图链接: http://i.stack.imgur.com/xJRAb.png
但是,我想检索 1个或更多产品。如果我将我的查询设置为返回1个或更多产品..列的顺序混乱并且看起来像截图链接: http://i.stack.imgur.com/xHRTg.png
这是我使用的查询:
SELECT * FROM(
SELECT bso.ID AS meta_id, bso.ID AS order_item_id, '_qty' AS meta_key, 1 AS meta_value
FROM b_sale_order bso
WHERE bso.ID IN(832, 830)
UNION
SELECT bso.ID AS meta_id, bso.ID AS order_item_id, '_tax_class' AS meta_key, '' AS meta_value
FROM b_sale_order bso
WHERE bso.ID IN(832, 830)
UNION
SELECT bsb.ORDER_ID AS meta_id, bsb.ORDER_ID AS order_item_id, '_product_id' AS meta_key, bsb.PRODUCT_ID AS meta_value
FROM b_sale_basket bsb
WHERE bsb.ORDER_id IN(832, 830)
UNION
SELECT bso.ID AS meta_id, bso.ID AS order_item_id, '_variation_id' AS meta_key, '' AS meta_value
FROM b_sale_order bso
WHERE bso.ID IN(832, 830)
UNION
SELECT bso.ID AS meta_id, bso.ID AS order_item_id, '_line_subtotal' AS meta_key, '' AS meta_value
FROM b_sale_order bso
WHERE bso.ID IN(832, 830)
UNION
SELECT bso.ID AS meta_id, bso.ID AS order_item_id, '_line_total' AS meta_key, '' AS meta_value
FROM b_sale_order bso
WHERE bso.ID IN(832, 830)
UNION
SELECT bso.ID AS meta_id, bso.ID AS order_item_id, '_line_tax' AS meta_key, 0 AS meta_value
FROM b_sale_order bso
WHERE bso.ID IN(832, 830)
UNION
SELECT bso.ID AS meta_id, bso.ID AS order_item_id, '_line_subtotal_tax' AS meta_key, 0 AS meta_value
FROM b_sale_order bso
WHERE bso.ID IN(832, 830)
UNION
SELECT bso.ID AS meta_id, bso.ID AS order_item_id, 'method_id' AS meta_key, bsd.NAME AS meta_value
FROM b_sale_order bso
LEFT OUTER JOIN b_sale_delivery bsd ON bso.DELIVERY_ID = bsd.ID
WHERE bso.ID IN (832, 830)
UNION
SELECT bso.ID AS meta_id, bso.ID AS order_item_id, 'cost' AS meta_key, bsd.PRICE AS meta_value
FROM b_sale_order bso
LEFT OUTER JOIN b_sale_delivery bsd ON bso.DELIVERY_ID = bsd.ID
WHERE bso.ID IN (832, 830)
) sales_order_meta
注意:我仍然希望列的排序看起来像第一个链接/图像,即使有一个或多个产品返回。
请帮助我。
答案 0 :(得分:0)
我假设你的意思是排序,而不是列。如果需要特定顺序的行,则必须使用order by语句,否则DBMS可以按任意顺序返回行。即。
select ... from (
...
LEFT OUTER JOIN b_sale_delivery bsd ON bso.DELIVERY_ID = bsd.ID
WHERE bso.ID IN (832, 830)
) sales_order_meta
order by ...
作为旁注,请勿使用select *
如果您想按meta_id排列结果meta_id,并且以反映联合顺序的方式,您可以为每个联合添加一个排序属性(比如origin),如:
SELECT ... FROM (
SELECT bso.ID AS meta_id, bso.ID AS order_item_id, '_qty' AS meta_key, 1 AS meta_value
, 1 as origin
FROM b_sale_order bso
WHERE bso.ID IN(832, 830)
UNION
SELECT bso.ID AS meta_id, bso.ID AS order_item_id, '_tax_class' AS meta_key, '' AS meta_value
, 2 as origin
FROM b_sale_order bso
WHERE bso.ID IN(832, 830)
UNION
...
)
order by meta_id, origin
出于效率原因,我建议如下:
SELECT ... FROM(
SELECT bso.ID AS meta_id, bso.ID AS order_item_id
, x.meta_key, x.meta_value, x.meta_key_order
FROM b_sale_order bso
CROSS JOIN ( select '_qty' AS meta_key, 1 AS meta_value, 1 as meta_key_order
union
select '_tax_class' AS meta_key, '' AS meta_value, 2 as meta_key_order
union
select '_variation_id' AS meta_key, '' AS meta_value, 4 as meta_key_order
union
select '_line_subtotal' AS meta_key, '' AS meta_value, 5 as meta_key_order
union
...
) as x
WHERE bso.ID IN(832, 830)
UNION
-- 8
SELECT bso.ID AS meta_id, bso.ID AS order_item_id, 'method_id' AS meta_key, bsd.NAME AS meta_value, 8 as meta_key_order
FROM b_sale_order bso
LEFT OUTER JOIN b_sale_delivery bsd ON bso.DELIVERY_ID = bsd.ID
WHERE bso.ID IN (832, 830)
UNION
-- 9
SELECT bso.ID AS meta_id, bso.ID AS order_item_id, 'cost' AS meta_key, bsd.PRICE AS meta_value, 9 as meta_key_order
FROM b_sale_order bso
LEFT OUTER JOIN b_sale_delivery bsd ON bso.DELIVERY_ID = bsd.ID
WHERE bso.ID IN (832, 830)
-- 3
SELECT bsb.ORDER_ID AS meta_id, bsb.ORDER_ID AS order_item_id, '_product_id' AS meta_key, bsb.PRODUCT_ID AS meta_value, 3 as meta_key_order
FROM b_sale_basket bsb
WHERE bsb.ORDER_id IN(832, 830)
) sales_order_meta
order by meta_id, meta_key_order
答案 1 :(得分:0)
对order by
s:
join
SELECT * FROM(
...
) sales_order_meta
ORDER BY order_item_id, meta_key