MYSQL UNION and ORDER BY not working
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
Try
order by op_name
instead of
ORDER BY order_product.op_name
SELECT *
FROM (SELECT order_product.op_id,
...
FROM
... )
UNION
(SELECT order_product.op_id,
...
FROM order_product
... ) U
ORDER BY op_name;
精彩评论