SQL `order`.id 未知列中的变量范围
在以下查询中,INNER JOIN 中的 order.id 引用引发了错误 Unknown column 'order.id' in 'on Clause'
。 id
列确实存在。
有什么想法为什么不能通过子查询访问它吗?
SELECT
SUM(price+shipping_price) AS total_sales,
COUNT(id) AS total_orders,
AVG(price+shipping_price) AS order_total_average,
(SELECT
SUM(quantity)
FROM `order_product`
WHERE `order`.id = order_product.order_id
) as total_units,
SUM(price+shipping_price)/7 as daily_average,
(SELECT
SUM(order_product.price * order_return_product.quantity)
FROM order_return_product
INNER JOIN order_product ON (
order_product.order_id = `order`.id AND
order_product.product_id = order_return_product.product_id AND
order_product.vehicle_id = order_return_product.vehicle_id
)
WHERE return_id IN (
SELECT
id
FROM order_return
WHERE status_id != 3 AND
order_return.order_id = `order`.id
)
) as total_returns
FROM `order`
WHERE created >= 1278388801 AND
created <= 1279079999 AND
fraud = 0 AND
type_id = 4
当我注释掉 INNER JOIN 中的 order
.id 时,我没有收到任何错误
In the following query, the error Unknown column 'order.id' in 'on clause'
is being thrown by my order.id reference in the INNER JOIN. The id
column does indeed exist.
Any ideas why it's not accessible via the sub query?
SELECT
SUM(price+shipping_price) AS total_sales,
COUNT(id) AS total_orders,
AVG(price+shipping_price) AS order_total_average,
(SELECT
SUM(quantity)
FROM `order_product`
WHERE `order`.id = order_product.order_id
) as total_units,
SUM(price+shipping_price)/7 as daily_average,
(SELECT
SUM(order_product.price * order_return_product.quantity)
FROM order_return_product
INNER JOIN order_product ON (
order_product.order_id = `order`.id AND
order_product.product_id = order_return_product.product_id AND
order_product.vehicle_id = order_return_product.vehicle_id
)
WHERE return_id IN (
SELECT
id
FROM order_return
WHERE status_id != 3 AND
order_return.order_id = `order`.id
)
) as total_returns
FROM `order`
WHERE created >= 1278388801 AND
created <= 1279079999 AND
fraud = 0 AND
type_id = 4
I get no errors when I comment out order
.id within the INNER JOIN
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
order
.id 超出此查询的范围 - 您仅处理子查询中的order_return_product
和order_product
表。The
order
.id is out of scope in this query - you're only dealing with theorder_return_product
andorder_product
tables in the subquery.试试这个:
...对于total_returns子查询
try this:
... for the total_returns subquery
我认为问题是因为 order 是 mysql 的关键字所以使用
OR
I think problem is because order is keyword for mysql so use
OR