我有一个mysql查询,如下所示
(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
查询是givng错误,T
Table 'order_product' from one of the SELECTs cannot be used in global ORDER clause
我检查了MYSQL手册,但我没有得到任何线索,任何帮助都会非常棒。
答案 0 :(得分: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
顺便说一下:没有必要将UNION的各个SELECT放入括号中。
答案 1 :(得分:3)
请尝试以下语法?
SELECT
x, y
FROM
(
SELECT x, y FROM z
UNION
SELECT a, b FROM c
)
ORDER BY
x, y
答案 2 :(得分:2)
尝试
order by op_name
而不是
ORDER BY order_product.op_name
答案 3 :(得分:0)
SELECT *
FROM (SELECT order_product.op_id,
...
FROM
... )
UNION
(SELECT order_product.op_id,
...
FROM order_product
... ) U
ORDER BY op_name;