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.
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.
Try the following syntax?
SELECT
x, y
FROM
(
SELECT x, y FROM z
UNION
SELECT a, b FROM c
)
ORDER BY
x, y
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With