SQL`order`.id未知列中的变量范围

时间:2010-07-13 13:14:40

标签: sql mysql mysql-error-1054

在以下查询中,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时,我没有收到任何错误

3 个答案:

答案 0 :(得分:2)

order。id超出了此查询的范围 - 您只处理子查询中的order_return_productorder_product表。

答案 1 :(得分:0)

我认为问题是因为order是mysql的关键字所以使用

`order`.`id `

OR

   o.id
   |
   |
   |
  FROM `order` o

答案 2 :(得分:0)

试试这个:

SELECT
    SUM(order_product.price * order_return_product.quantity)
FROM order_return_product
INNER JOIN order_product ON (
    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
) 
AND order_product.order_id = `order`.id 

...用于total_returns子查询