Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MYSQL UNION and ORDER BY not working

Tags:

sql

mysql

union

I have a mysql query which is as follows

(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

The query is givng error, T

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

I checked the MYSQL manual, but am not getting any clue, any help would be really great.

like image 481
Amitabh Avatar asked Mar 16 '11 15:03

Amitabh


2 Answers

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

Btw: there is no need to put the individual SELECTs of a UNION into brackets.

like image 178
a_horse_with_no_name Avatar answered Sep 25 '22 09:09

a_horse_with_no_name


Try the following syntax?

SELECT
  x, y
FROM
(
  SELECT x, y FROM z
  UNION
  SELECT a, b FROM c
)
ORDER BY
  x, y
like image 24
MatBailie Avatar answered Sep 26 '22 09:09

MatBailie