我可以在左外连接与普通表中使用SQL视图吗?

时间:2018-10-12 10:05:42

标签: mysql sql join sql-view

这是我的观点

CREATE OR REPLACE VIEW w AS SELECT
orp1.order_product_id,
orp1.order_id,
pov1.weight,
sum(orp1.quantity) AS 'quantity'
FROM
`order_product` orp1,
`order_option` oro1,
`product_option_value` pov1
WHERE
pov1.option_id = 6 AND oro1.product_option_value_id = pov1.product_option_value_id AND orp1.order_product_id = oro1.order_product_id
GROUP BY
orp1.order_product_id,
orp1.order_id,
pov1.weight;

这是查询部分

FROM 
    `order_product` orp6,
    `product` p,
    `product_description` pd,
    `product_to_category` p2c
LEFT OUTER JOIN w ON orp6.order_id = w.order_id AND orp6.order_product_id = w.order_product_id

但是显示的错误是:

  

orp6.order_id 是未知列

1 个答案:

答案 0 :(得分:1)

从不function(jqXHR,textStatus,errorThrown ){} 子句中使用逗号。 始终使用正确的,明确的,标准 FROM语法。在这个

JOIN

然后您可以在CREATE OR REPLACE VIEW w AS SELECT orp1.order_product_id, orp1.order_id, pov1.weight, sum(orp1.quantity) AS 'quantity' FROM `order_product` orp1 JOIN `order_option` oro1 ON orp1.order_product_id = oro1.order_product_id JOIN `product_option_value` pov1 ON oro1.product_option_value_id = pov1.product_option_value_id WHERE pov1.option_id = 6 GROUP BY orp1.order_product_id, orp1.order_id, pov1.weight; 子句中添加其他JOIN,例如要添加的子句。