2011-03-16 15 views
5

que tienen una consulta mysql es el siguienteMYSQL Union y la orden por no trabajar

(SELECT order_product.op_id, 
     order_product.ocat_id, 
     order_product.op_partnunber, 
     order_product.op_name, 
     order_product.op_upc, 
     order_product.op_desc, 
     order_stockavailable.osa_id, 
     order_stockavailable.of_id, 
     order_stockavailable.osa_stocka, 
     order_category.ocat_name 
FROM 
    order_product 
    LEFT JOIN order_category 
    ON order_product.ocat_id = order_category.ocat_id 
    LEFT JOIN order_stockavailable 
    ON order_product.op_id = order_stockavailable.op_id) 
UNION 
(SELECT order_product.op_id, 
     order_product.ocat_id, 
     order_product.op_partnunber, 
     order_product.op_name, 
     order_product.op_upc, 
     order_product.op_desc, 
     order_stockavailable_attributes.id, 
     order_stockavailable_attributes.of_id, 
     order_stockavailable_attributes.opap_stock, 
     order_category.ocat_name 
FROM order_product 
    LEFT JOIN order_category 
    ON order_product.ocat_id = order_category.ocat_id 
    LEFT JOIN order_stockavailable 
    ON order_product.op_id = order_stockavailable.op_id 
    LEFT JOIN order_stockavailable_attributes 
    ON order_product.op_id = order_stockavailable_attributes.op_id) 
ORDER BY order_product.op_name 

La consulta se error givng, T

Table 'order_product' from one of the SELECTs cannot be used in global ORDER clause

he comprobado el manual de MySQL, pero estoy sin obtener ninguna pista, cualquier ayuda sería realmente genial.

Respuesta

13
 
SELECT * 
FROM (
    SELECT order_product.op_id, 
      order_product.ocat_id, 
      order_product.op_partnunber, 
      order_product.op_name, 
      order_product.op_upc, 
      order_product.op_desc, 
      order_stockavailable.osa_id, 
      order_stockavailable.of_id, 
      order_stockavailable.osa_stocka, 
      order_category.ocat_name 
    FROM 
     order_product 
     LEFT JOIN order_category 
     ON order_product.ocat_id = order_category.ocat_id 
     LEFT JOIN order_stockavailable 
     ON order_product.op_id = order_stockavailable.op_id 
    UNION 
    SELECT order_product.op_id, 
      order_product.ocat_id, 
      order_product.op_partnunber, 
      order_product.op_name, 
      order_product.op_upc, 
      order_product.op_desc, 
      order_stockavailable_attributes.id, 
      order_stockavailable_attributes.of_id, 
      order_stockavailable_attributes.opap_stock, 
      order_category.ocat_name 
    FROM order_product 
     LEFT JOIN order_category 
     ON order_product.ocat_id = order_category.ocat_id 
     LEFT JOIN order_stockavailable 
     ON order_product.op_id = order_stockavailable.op_id 
     LEFT JOIN order_stockavailable_attributes 
     ON order_product.op_id = order_stockavailable_attributes.op_id 
) t 
ORDER BY op_name 

Por cierto: no hay necesidad de poner los SELECT individuales de una unión en los soportes.

+2

más uno para la batalla contra los soportes innecesarias. –

+0

gracias esto funcionó a la perfección! los corchetes formaban parte del proceso de prueba y error, estaba probando varias combinaciones antes de publicarlas aquí. Ahora necesito resolver el misterio de t :) – Amitabh

+0

@ pilar.del.autumn: el 't' es un alias para la consulta interna. –

2

Trate

order by op_name 

en lugar de

ORDER BY order_product.op_name 
3

Prueba la siguiente sintaxis?

SELECT 
    x, y 
FROM 
(
    SELECT x, y FROM z 
    UNION 
    SELECT a, b FROM c 
) 
ORDER BY 
    x, y 
+1

¡genial! solo necesito un alias de subconsulta y es perfecto – bernie

0
SELECT * 
FROM (SELECT order_product.op_id, 
     ... 
     FROM 
     ...) 
     UNION 
     (SELECT order_product.op_id, 
     ... 
     FROM order_product 
     ...) U 
ORDER BY op_name;